0

I am updating my profile image using retrofit. i have to send the object of image and current login user id here is my code ...

private void uploadProfileImage(){
    uid = DatabaseUtil.getInstance().getUser().getProfile().getUmeta().getId();
    mRegProgress.setTitle("Updating profile Image");
    mRegProgress.setMessage("Please wait...");
    mRegProgress.setCanceledOnTouchOutside(false);
    mRegProgress.show();

    File profile_image_file = new File(mediaPath);

    RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), profile_image_file);
    MultipartBody.Part profile_image = MultipartBody.Part.createFormData("file", profile_image_file.getName(), requestBody);

    Call<ResponseBody> call = RetrofitClient.getInstance().getApi().uploadProfile(uid , profile_image);
    call.enqueue(new Callback<ResponseBody>() {
        @Override
        public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

            if (response.code() == 200){
                mRegProgress.hide();
                String s  = response.body().toString();
                pDialog = new SweetAlertDialog(getActivity(), SweetAlertDialog.SUCCESS_TYPE);
                pDialog.setTitleText("Good job!");
                pDialog.setContentText("Profile image successfully!");
                pDialog.show();
            }else if (response.code() == 203){
                Toast.makeText(getActivity() , "Image upload Error" , Toast.LENGTH_SHORT).show();

            }
        }

        @Override
        public void onFailure(Call<ResponseBody> call, Throwable t) {
            t.printStackTrace();
        }
    });
}

also i am using this code as well...

case SELECT_PROFILE_PIC:
    if (resultCode == RESULT_OK) {


        // Get the Image from data
        Uri selectedImage = data.getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};

        Cursor cursor = getActivity().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        assert cursor != null;
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        mediaPath = cursor.getString(columnIndex);

        // Set the Image in ImageView for Previewing the Media
            dd_profile_view.setImageBitmap(BitmapFactory.decodeFile(mediaPath));
        cursor.close();

        uploadProfileImage();
    }
    break;

this is what i am sending in my code

but the image is not updating... i have tried api on post man it is updating the profile pic correctly ... kindly tell me what is the issue and how can i solve it Thanks

here is my api call..

@Multipart
@POST("media/upload-media")
Call<ResponseBody> uploadProfile(
    @Query("id") String id,
    @Part MultipartBody.Part profile_image
);

i am adding reproce getting from post man.. this is the locked response of post man

S.Hashmi
  • 485
  • 1
  • 8
  • 29

2 Answers2

2

Try using @Part in placeof @Query

@Multipart
@POST("media/upload-media")
Call<ResponseBody> uploadProfile(
        @Part("id") RequestBody id,
        @Part MultipartBody.Part profile_image
);

Replace

RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), profile_image_file);

With :

RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), profile_image_file);

you can follow this Link for complete Explanationenter link description here

  • As to why this works: Looking at the Postman request, the server expects both the id and profile_image to be in the POST request body. Which means you can't pass `id` as a query parameter which would end up in `$_GET` in PHP – JensV Apr 12 '19 at 10:11
  • @JensV can you please explain – S.Hashmi Apr 12 '19 at 10:52
0

please update below the line

 RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), profile_image_file);
 MultipartBody.Part profile_image = MultipartBody.Part.createFormData("file", profile_image_file.getName(), requestBody);

instead of this

     RequestBody requestBody= RequestBody.create(MediaType.parse("image/*"), profile_image_file);
MultipartBody.Part profile_image = MultipartBody.Part.createFormData("file", profile_image_file.getName(), requestBody);
     RequestBody uid= RequestBody.create(MediaType.parse("text/plain"), uid);

And make sure use @part instead of this @Query

@Multipart
@POST("media/upload-media")
Call<ResponseBody> uploadProfile(
        @Part("id") RequestBody id,
        @Part MultipartBody.Part profile_image
);
Haresh Ramani
  • 390
  • 1
  • 16