【问题标题】:Image uploading from android to PHP server using retrofit使用改造将图像从 android 上传到 PHP 服务器
【发布时间】:2019-02-14 03:26:05
【问题描述】:

我正在使用改造将图像上传到服务器。我正在将图像编码为位图,然后将位图转换为字符串并将字符串传递给 PHP。在 PHP 端,我再次解码图像,然后保存到服务器文件夹。 如果我将图像质量压缩到 30,它可以完美运行,但如果我将图像质量设置为 100,应用程序会崩溃并显示空指针。

这是我的代码:

结果活动:

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();

        }

位图到字符串:

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;


}

改造 API 调用:

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

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

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

【问题讨论】:

    标签: java php android json retrofit2


    【解决方案1】:

    我建议将位图作为二进制数据发送,而不是与字符串进行转换。例如:

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

    然后是这样的:

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

    【讨论】:

    • 感谢@John。请您添加一些代码以了解如何在 php 中获取它并将其保存到服务器文件夹?
    • @AsifUllah 我对 PHP 不是很熟悉,但假设必须有非常标准的方法来执行此操作。从快速谷歌上,我看到了几篇不错的文章(尽管主要与使用 Multipart Posts 相关,这也是您可以采取的另一种方法。
    • @AsifUllah 听从 John 的建议,参考这个 PHP 的官方 documentation for uploading files
    【解决方案2】:

    尝试在远离主 UI 线程的单独线程中执行 BitmaptoString() 操作。

    如果您在主 UI 线程中执行位图处理成本太高,应用程序可能会崩溃。此外,您可以使用Asynctask 或任何后台进程来执行昂贵的功能并避免在主线程中进行任何昂贵的操作。

    【讨论】:

      猜你喜欢
      • 2018-10-05
      • 2021-07-21
      • 2018-04-03
      • 2011-02-02
      • 2015-03-29
      • 2011-10-15
      • 2018-02-19
      相关资源
      最近更新 更多