Java 如何在改造中减小多个图像的大小

Java 如何在改造中减小多个图像的大小,java,android,retrofit2,Java,Android,Retrofit2,我正在尝试使用改型将多个图像上载到服务器,而我上载小图像一点问题都没有,但如果上载大图像,则会出现超时错误。这是我的密码 File file1 = new File(getRealPathFromDocumentUri(this, selected.get(0))); File file2 = new File(getRealPathFromDocumentUri(this, selected.get(1))); RequestBody reqFile1 = RequestBody.

我正在尝试使用改型将多个图像上载到服务器,而我上载小图像一点问题都没有,但如果上载大图像,则会出现超时错误。这是我的密码

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

要减小图像大小,您需要执行一些步骤

首先,您需要从原始图像URL获取位图

位图缩放位图=位图工厂.decodeFile(imageFile.getAbsolutePath(),选项)

其次,根据您的选项缩放位图

scaledBitmap=Bitmap.createBitmap(scaledBitmap,0,0,scaledBitmap.getWidth(),scaledBitmap.getHeight(),矩阵,true)

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


如果你想使用这个开放库()

我想你应该把它留给用户。例如,在许多应用程序中,要上载的最大文件大小为3MB到5MB。你可以考虑在这方面做些限制。
     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;
}