Home » Android » android – Why Image is not showing on the server when uploaded with Retrofit Multipart?-Exceptionshub

android – Why Image is not showing on the server when uploaded with Retrofit Multipart?-Exceptionshub

Posted by: admin February 26, 2020 Leave a comment

Questions:

I am uploading a image on the server with Retrofit Multipart.

When I send the request, it is creating an image along with the passed name and size.

But when I try to open that image in the browser it says image contains error.

Same case was happening when I tried to send a request with the postman. But I found that I am sending wrong parameters. So I changed Content-Type to application/binary and posted image under
Body-> binary and it worked.

So I am suspecting that I am not sending proper Content-Type through the app. But I checked and found nothing

From ApiInterface.java

@Multipart
@POST("wp-json/wp/v2/media/")
Call<ImagePostResult> uploadImage(@Header("Authorization") String authHeader,
                                  @Header("Content-Type") String contentType,
                                  @Header("Content-Disposition") String contentDisposition,
                                  @Part MultipartBody.Part file,
                                  @Part("description") RequestBody description);

my request:

private void uploadIdentityProofImageToServer() {
    progressDialog.setMessage("Uploading identity proof");

    ApiInterface apiInterface = getApiInterfaceObj();

    // For BasicAuth
    String authHeader = getAuthHeader();

    //File creating from selected URL
    String imageLocalPath = orderDetailsArrayList.get(getAdapterPosition()).getCapturedPhotoPath();//encoded image
    Uri tempUri = orderDetailsArrayList.get(getAdapterPosition()).getTempUri();// uri of image

    File file = new File(imageLocalPath);

    // create RequestBody instance from file
    RequestBody requestBodyFile = RequestBody.create(MediaType.parse(mContext.getContentResolver().getType(tempUri)), file);

    String fileName = file.getName();
    MultipartBody.Part body = MultipartBody.Part.createFormData("picture", fileName, requestBodyFile);

    String descriptionString = "Sample description";
    RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"), descriptionString);

    String contentType = "application/binary";
    String contentDisposition = "attachment; filename =  " + fileName;


    Call<ImagePostResult> resultCall = apiInterface.uploadImage(
            authHeader,
            contentType,
            contentDisposition,
            body,
            description);

    resultCall.enqueue(new Callback<ImagePostResult>() {
        @Override
        public void onResponse(Call<ImagePostResult> call, Response<ImagePostResult> response) {
            // Response Success
            if (response.isSuccessful()) {
                ImagePostResult imagePostResult = response.body();
                String raw = imagePostResult.getGuid().getRaw();

                updateThePathOfImage(raw);
            }
            Log.d(TAG, "onResponse: " + response.message());
        }

        @Override
        public void onFailure(Call<ImagePostResult> call, Throwable t) {
            Log.d(TAG, "onFailure: " + t);
            progressDialog.dismiss();
            Toast.makeText(mContext, "" + t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });
}

Also if can you link any resources to upload image with binary format, it would be helpful.

How to&Answers:

I found it

I followed the solution code which is given at the end of this issue

Here is my little modified code

@POST("wp-json/wp/v2/media/")
Call<ImagePostResult> postEventPhoto(
        @Header("Authorization") String accessToken,
        @Header("Content-Type") String contentType,
        @Header("Content-Disposition") String contentDisposition,
        @Body RequestBody photo);

Here is the request

        // For BasicAuth
        String authHeader = getAuthHeader();

        String contentType = "application/binary";
        String contentDisposition = "attachment; filename =  " + fileName;

        RequestBody requestBodyee = null;
        try {
            InputStream in = new FileInputStream(file);

            byte[] buf;
            buf = new byte[in.available()];
            while (in.read(buf) != -1) ;
            requestBodyee = RequestBody
                    .create(MediaType.parse("application/octet-stream"), buf);
        } catch (IOException e) {
            e.printStackTrace();
        }


        Call<ImagePostResult> imagePostResultCall = apiInterface.postEventPhoto(
                authHeader,
                contentType,
                contentDisposition,
                requestBodyee);
        imagePostResultCall.enqueue(new Callback<ImagePostResult>() {
            @Override
            public void onResponse(Call<ImagePostResult> call, Response<ImagePostResult> response) {
                // Response Success
                if (response.isSuccessful()) {
                  // yaay
                }
            }

            @Override
            public void onFailure(Call<ImagePostResult> call, Throwable t) {
                Log.d(TAG, "onFailure: " + t);
            }
        });