How do I insert the Bitmap file into mysql?

Asked 2 years ago, Updated 2 years ago, 40 views

I am creating a mobile community application application.

Currently, I logged in with Facebook and received the profile picture in Bitmap format.

I want to save the member's information in mysql, but I want to convert the Bitmap format into a String and save it

The length of the harness string is over 3900.

I tried to deliver it through php, but it was not delivered.

How can I save the Bitmap file format to mysql?

Below is the httpConnect code part that uploads from php and Android.

...
        $profilePic=$_GET['userImageString'];
        $con=mysqli_connect ("###","###","###","###") or die ("inaccessible";
            #require_once('dbConnect.php');         
        $sql = "INSERT INTO songDB(username,userID,songName,contents,filepath,profilePic) 
                VALUES('$username','$userID','$songName','$contents','$filepath','$profilePic')";
    if(mysqli_query($con,$sql)){
        echo 'successfully Upload';
    }else{
        echo 'oops! Please try again!';
    }      

    private void uploadSongDB() {    
         ...
        Bitmap userImage=MainActivity.UserIDClass.getUserImage();
        String userImageString=BitMapToString(userImage);

        String urlSuffix = "?username="+userName+"&userID="+userID+"&songName="+songName+"&contents="
                      +contents+"&filepath="+filepath+"&userImageString"+userImageString;

        class RegisterUser extends AsyncTask<String, Void, String>{

            ProgressDialog loading;

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
                loading = ProgressDialog.show(RecordActivity.this, "Please Wait",null, true, true);
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                loading.dismiss();
                if(s.equals("successfully Upload")) {
                    Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();
                    Intent intent = new Intent(RecordActivity.this, UploadActivity.class);        
                    intent.putExtra("path", MainActivity.UserIDClass.getUploadFilepath());
                    startActivity(intent);
                    finish();

                }
                else{
                    Toast.makeText(getApplicationContext(),s,Toast.LENGTH_LONG).show();

                }
            }

            @Override
            protected String doInBackground(String... params) {
                String s = params[0];
                BufferedReader bufferedReader = null;
                try {
                    URL url = new URL(UPLOAD_URL+s);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));

                    String result;

                    result = bufferedReader.readLine();

                    return result;
                }catch(Exception e){
                    return null;
                }
            }
        }

        RegisterUser ru = new RegisterUser();
        ru.execute(urlSuffix);
    }


android mysql php

2022-09-22 21:52

1 Answers

A more common method is to save the bitmap file uploaded by Android as a jpg file on the server and then save the path of the image file in mysql. It is common for clients (androids) to re-use their profile images to receive a path on the web.

For example, if the hashcode server provides my profile picture in url as follows

https://res.cloudinary.com/eightcruz/image/upload/c_lfill,h_32,w_32/qb0mjgu78t3hqjnztfr4

The implementation on Android is as follows:

Picasso.with(getApplicationContext())
       .load("https://res.cloudinary.com/eightcruz/image/upload/c_lfill,h_32,w_32/qb0mjgu78t3hqjnztfr4)
       .into(photoView);

Additionally, url is utilized as the key to the memory and file cache implemented by the image loader libraries. If you don't have to save the bitmap in mysql, why don't you change the structure in the same way as above?


2022-09-22 21:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.