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>() {
        public void onResponse(Call<PostComplaint> call, Response<PostComplaint> response) {


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

            } else {

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


        public void onFailure(Call<PostComplaint> call, Throwable t) {
            Log.d("Error", "" + t.getMessage());


API Interface

Call<PostComplaint> postComplaint(@Part MultipartBody.Part file,
                               @Part("photo") RequestBody name,
                               @PartMap Map<String, RequestBody> fields);

public class APIClient {

    private static Retrofit retrofit = null;

    static Retrofit getClient() {

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

        retrofit = new Retrofit.Builder()

        return retrofit;




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