如何在Retrofit中减小多个图像的大小

时间:2018-02-25 11:25:10

标签: java android retrofit2

我正在尝试使用Retrofit将多个图像上传到服务器,虽然我上传的小图片完全没有问题但是如果我上传大图片则会出现超时错误。这是我的代码

  File file1 = new File(getRealPathFromDocumentUri(this, selected.get(0)));
  File file2 = new File(getRealPathFromDocumentUri(this, selected.get(1)));
  RequestBody reqFile1 = RequestBody.create(MediaType.parse("image/*"), file1);
  RequestBody reqFile2 = RequestBody.create(MediaType.parse("image/*"), file2);
  MultipartBody.Part body1 = MultipartBody.Part.createFormData("image1", file1.getName(), reqFile1);
  MultipartBody.Part body2 = MultipartBody.Part.createFormData("image2", file1.getName(), reqFile2);

    RequestBody premium1 = RequestBody.create(MediaType.parse("text/plain"), premium);
    RequestBody featured1 = RequestBody.create(MediaType.parse("text/plain"), featured);


    Call<ResponseBody> call = api.getPostedResult(premium1, featured1, body1, body2);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
            //  pb.dismiss();utils
            utils.dismissProgress();
            if (response.code() == 200) {


                Toast.makeText(getApplicationContext(), "Your AD Have been Posted Sucessfully", Toast.LENGTH_LONG).show();
                Intent intent = null;
                intent = new Intent(getApplicationContext(), LandingPage.class);
                startActivity(intent);


            } else {

                Toast.makeText(getApplicationContext(), "" + response.code(), Toast.LENGTH_LONG).show();
            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            //
            utils.dismissProgress();
            Toast.makeText(getApplicationContext(), "" + t.getMessage(), Toast.LENGTH_LONG).show();
        }
    });

我正在使用我的列表选择文件路径并将其发送到下面的代码,请帮助我,我想知道如何减少图像大小并将其发送到Multipart.body中的服务器

     public static String getRealPathFromDocumentUri(Context context, Uri uri) {
    String filePath = "";

    Pattern p = Pattern.compile("(\\d+)$");
    Matcher m = p.matcher(uri.toString());
    if (!m.find()) {
        //    Log.e(ImageConverter.class.getSimpleName(), "ID for requested image not found: " + uri.toString());
        return filePath;
    }
    String imgId = m.group();

    String[] column = {MediaStore.Images.Media.DATA};
    String sel = MediaStore.Images.Media._ID + "=?";

    Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            column, sel, new String[]{imgId}, null);

    int columnIndex = cursor.getColumnIndex(column[0]);

    if (cursor.moveToFirst()) {
        filePath = cursor.getString(columnIndex);
    }
    cursor.close();

    return filePath;
}

1 个答案:

答案 0 :(得分:1)

要减小图像尺寸,您需要执行几个步骤。

首先,您需要从原始图片网址

获取BitMap

位图scaledBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(),options);

第二,根据您的选项

缩放位图

scaledBitmap = Bitmap.createBitmap(scaledBitmap,0,0,scaledBitmap.getWidth(),scaledBitmap.getHeight(),matrix,true);

然后将图像保存到临时文件并使用此URL进行上传。

如果您需要,请使用此打开的库(https://github.com/amanjeetsingh150/ImageZipper