Search code examples
androidimageretrofit2multipart

How to upload Image using retrofit2 multipart?


I totally got struct in uploading an image on server. I'm getting response from server successfully but image is not getting upload on server. Always asking for "Insert Your Image".

here is my code please check this, where i'm making mistake

private void uploadFile(Uri fileUri) {
    File file = new File(fileUri.getPath());
    if (file.exists()) {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        Log.e(TAG, "uploadFile:File "+file );
        MediaType MEDIA_TYPE_PNG = MediaType.parse("multipart/form-data");

        APIInterface service = retrofit.create(APIInterface.class);

        RequestBody requestFile = RequestBody.create(MEDIA_TYPE_PNG, file);
        Log.e(TAG, "uploadUri: " + strEmpsno + " " + strStoreSno + "  " + strLrno + "  " + strRecqty + "  " + strDeliverydate + " " + strDeliverytime);
        Call<PODResponse> call = service.postFile(strEmpsno, strStoreSno, strLrno, strRecqty, strRecvol,
                strRecwgt, strDamageqty, strLooseqty, strDeliverydate
                , strDeliverytime, requestFile, strRemarks, strReceivedby,
                strIpaddress);

        call.enqueue(new Callback<PODResponse>() {
            @Override
            public void onResponse(Response<PODResponse> response) {
                Log.e(TAG, "onResponse:uploadUri " + response.isSuccess());
                if (response.isSuccess()) {
                    Log.e(TAG, "uploadUri: " + response.body().getResult());
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e(TAG, "onFailure:uploadUri " + t.getLocalizedMessage());
            }
        });
    }
  }

In log of Log.e(TAG, "uploadFile:File "+file ); i'm getting

/storage/emulated/0/Audex/saved_images/logo.jpg

This is my Image name with path. From Rest(chrome extension) image is successfully getting stored on server but not by using this code. Please help me.

My Interface is like

  @Multipart
@POST("/savePic")
Call<PODResponse> postFile(
        @Part("empsno") String empsno,
        @Part("storesno") String storesno,
        @Part("lrSno") String lrSno,
        @Part("recQty") String recQty,
        @Part("recVol") String recVol,
        @Part("recWgt") String recWgt,
        @Part("damageQty") String damageQty,
        @Part("looseQty") String looseQty,
        @Part("deliveryDate") String deliveryDate,
        @Part("deliveryTime") String deliveryTime,
        @Part("uploadFile\"; filename=\"audex.jpg\" ") RequestBody part,
        @Part("remarks") String remarks,
        @Part("receivedBy") String receivedBy,
        @Part("ipAddress") String ipAddress
);

Thank you All in advance


Solution

  • After long long time i got my answer. Thanks all who tried for me. Only error was i was putting RequestBody instead of String.

      @Multipart
      @POST("/savePic")
      Call<PODResponse> postFile(
        @Part("empsno") String empsno,
        @Part("storesno") String storesno,
        @Part("lrSno") String lrSno,
        @Part("recQty") String recQty,
        @Part("recVol") String recVol,
        @Part("recWgt") String recWgt,
        @Part("damageQty") String damageQty,
        @Part("looseQty") String looseQty,
        @Part("deliveryDate") String deliveryDate,
        @Part("deliveryTime") String deliveryTime,
        @Part("uploadFile\"; filename=\"audex.jpg\" ") String part, // Change here
        @Part("remarks") String remarks,
        @Part("receivedBy") String receivedBy,
        @Part("ipAddress") String ipAddress
        @Part MultipartBody.Part images
    );
    

    @Part("uploadFile\"; filename=\"audex.jpg\" ") String part simply i made changes from RequestBody to string and in last add @Part MultipartBody.Part images that's it.

    **Dependency I used is

    compile 'com.squareup.retrofit2:retrofit-converters:2.0.2'
    compile 'com.squareup.retrofit2:converter-gson:2.0.2'
    compile 'com.squareup.retrofit2:retrofit:2.0.2'
    compile 'com.squareup.retrofit2:converter-scalars:2.0.2'