简体   繁体   中英

Image uploading from android to PHP server using retrofit

I am uploading image to server using retrofit. I am encoding image to bitmap and then convert bitmap to string and passing string to PHP. On PHP side I decode image again and then save to server folder. It works perfectly if I compress image quality to 30 but app crashes and shows null pointer if I set image quality to 100.

Here is my Code:

ResultActivity:

if (requestCode == 1 && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = {MediaStore.Images.Media.DATA};
            Cursor cursor = getContentResolver().query(selectedImage, 
filePathColumn, null, null, null);
            cursor.moveToFirst();
            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            // photo = BitmapFactory.decodeFile(picturePath);


            profile_photo = 
ImageUtils.getInstant().getCompressedBitmap(picturePath);
            Uri tempUri = getImageUri(this, profile_photo);
            cursor.close();
            profile_image.setImageResource(android.R.color.transparent);
            Picasso.get()
                    .load(tempUri)
                    .resize(150, 150)
                    .into(profile_image);
            profile_image.setScaleType(ImageView.ScaleType.FIT_XY);
            profile_image.setPadding(5, 5, 5, 5);
            //Bitmap profile_photo = ((BitmapDrawable) 
profile_image.getDrawable()).getBitmap();
            upload_profileimage();

            b.dismiss();

        }

Bitmap to string:

public String BitmapTOString(Bitmap bitmap) {

    Bitmap bm = bitmap;
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] byteFormat = stream.toByteArray();
    String imgString = Base64.encodeToString(byteFormat, Base64.DEFAULT);
    return imgString;


}

Retrofit API call:

call = user_profileimge_interface.profileImage_uplaod(BitmapTOString(profile_photo), user_id);

PHP Code:

$data = $baseurl.'user_profile_pictures/'.$user_id.".JPEG";
file_put_contents($data, base64_decode($profile_picture));
echo json_encode(Array('message' => "image inserted"));

API interface:

@POST("update_profilepic.php")
Call<Profile_Image_updateJson> profileImage_uplaod(@Query("profile_picture") String profileImage,
                                                   @Query("user_id") String user_id);

I'd suggest sending bitmap as binary data rather than converting to/from string. For example:

@POST
Call<Profile_Image_updateJson> profileImage_uplaod(@Query("user_id") String user_id, @Body RequestBody body);

and then something like:

requestBody = RequestBody.create(MediaType.parse("image/jpeg"), imageBytes)
call = user_profileimge_interface.profileImage_uplaod(user_id, requestBody);

Try to Perform BitmaptoString() operation in a separate thread, away from the Main UI Thread.

As processing bitmap is too costly if you perform it in the Main UI Thread, the App might crash. Also, you can use Asynctask or Any Background Process to perform costly functions and avoid any costly operations in Main Thread.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM