简体   繁体   English

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

[英]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.我将图像编码为位图,然后将位图转换为字符串并将字符串传递给 PHP。 On PHP side I decode image again and then save to server folder.在 PHP 端,我再次解码图像,然后保存到服务器文件夹。 It works perfectly if I compress image quality to 30 but app crashes and shows null pointer if I set image quality to 100.如果我将图像质量压缩到 30,它可以完美运行,但如果我将图像质量设置为 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:改造 API 调用:

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

PHP Code: PHP代码:

$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: API接口:

@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.尝试在单独的线程中执行BitmaptoString()操作,远离主 UI 线程。

As processing bitmap is too costly if you perform it in the Main UI Thread, the App might crash.如果在主 UI 线程中执行位图处理成本太高,则应用程序可能会崩溃。 Also, you can use Asynctask or Any Background Process to perform costly functions and avoid any costly operations in Main Thread.此外,您可以使用Asynctask或 Any Background Process 来执行代价高昂的功能并避免在主线程中进行任何代价高昂的操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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