How to Compress the captured image file size before sending to server using retrofit Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of How to Compress the captured image file size before sending to server using retrofit without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am Using Retrofit retrofit:2.1.0′ to upload file image to server

If I take image using front camera image get uploaded successfully but if take back side camera its not uploaded i think because of large file size image not uploaded is there any option to compress the file size before sending to server in retrofit?

File Sending Coding

  map.put("complaint_category", RequestBody.create(parse("text"), caty_id_str.getBytes()));

    // Map is used to multipart the file using okhttp3.RequestBody
    File file = new File(Config.uriImage);
    // Parsing any Media type file
    RequestBody requestBody = RequestBody.create(parse("*/*"), file);
    MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("photo", file.getName(), requestBody);
    RequestBody filename = RequestBody.create(parse("text/plain"), file.getName());

    Call<PostComplaint> call3 = apiInterface.postComplaint(fileToUpload, filename, map);
    call3.enqueue(new Callback<PostComplaint>() {
        @Override
        public void onResponse(Call<PostComplaint> call, Response<PostComplaint> response) {

            progressDoalog.dismiss();

            PostComplaint respon = response.body();

            PostComplaint.Response respo = respon.getResponse();

            String result = respo.getResult();
            String data = respo.getData();
            if (result.equalsIgnoreCase("Success")) {

                Toast.makeText(getApplicationContext(), data, Toast.LENGTH_SHORT).show();
                Intent intent = new Intent(Activity_Post_Complaint.this, MainActivity.class);
                startActivity(intent);

            } else {

                Toast.makeText(getApplicationContext(), data, Toast.LENGTH_SHORT).show();
            }

        }

        @Override
        public void onFailure(Call<PostComplaint> call, Throwable t) {
            progressDoalog.dismiss();
            Log.d("Error", "" + t.getMessage());
            call.cancel();
        }


    });

API Interface

@Multipart
@POST("/somelink.php?")
Call<PostComplaint> postComplaint(@Part MultipartBody.Part file,
                               @Part("photo") RequestBody name,
                               @PartMap Map<String, RequestBody> fields);

APIClient.java

public class APIClient {

    private static Retrofit retrofit = null;

    static Retrofit getClient() {

        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor)
                .connectTimeout(60, TimeUnit.SECONDS)
                .readTimeout(60, TimeUnit.SECONDS)
                .writeTimeout(60, TimeUnit.SECONDS)
                .build();

        retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.1.135")
                .addConverterFactory(GsonConverterFactory.create())
                .client(client)
                .build();

        return retrofit;
    }

}

Answer

Try

int compressionRatio = 2; //1 == originalImage, 2 = 50% compression, 4=25% compress
File file = new File (imageUrl);
try {
    Bitmap bitmap = BitmapFactory.decodeFile (file.getPath ());
    bitmap.compress (Bitmap.CompressFormat.JPEG, compressionRatio, new FileOutputStream (file));
}
catch (Throwable t) {
    Log.e("ERROR", "Error compressing file." + t.toString ());
    t.printStackTrace ();
}
We are here to answer your question about How to Compress the captured image file size before sending to server using retrofit - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji