dra87370 2018-09-09 13:50 采纳率: 100%
浏览 92

图像使用改造从android上传到PHP服务器

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);
  • 写回答

1条回答 默认 最新

  • dsf8897 2018-09-09 13:56
    关注

    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);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?