0

here is the code for selecting Gallery Images

Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
startActivityForResult(intent, 2);
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 2 && data != null && data.getData() != null) {
Uri uri = data.getData();
uploadFile(uri);
}
}
}

The function below is used to send an image to the server using Retrofit. For uploading the images the MultipartBody is used for sending the image as a file and used RequestBody for sending String request as Params but getting the error in sending an image file from the gallery.

    private void uploadFile(Uri fileUri) {
            File file = new File(fileUri.getPath());
            long sTime = System.currentTimeMillis();
            MultipartBody.Part fbody = MultipartBody.Part.create(RequestBody.create(MediaType.parse("image/*"), file));

    RequestBody name = RequestBody.create(MediaType.parse("text/plain"),sTime + "FramePlayer.Jpeg");
    RequestBody id = RequestBody.create(MediaType.parse("text/plain"), "1498");
    RequestBody type = RequestBody.create(MediaType.parse("text/plain"), "player");
    RequestBody gif = RequestBody.create(MediaType.parse("text/plain"), "");    
    Api apiInterface = (Api) Client.getApi();
    Call<AddPlayerMemberModel> call = apiInterface.getResult(id, name, type, fbody, gif);
    call.enqueue(new Callback<AddPlayerMemberModel>() {
                @Override
                public void onResponse(Call<AddPlayerMemberModel> call, Response<AddPlayerMemberModel> response) {
                    if (!response.isSuccessful()) {
                        return;
                    }
                    AddPlayerMemberModel model1 = response.body();
                    Toast.makeText(MainActivity.this, "Image Frame Successfully", Toast.LENGTH_SHORT).show();
                    if (model1.getStatus().equals("Image Upload SuccessFully")) {

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

    Here is the Interface class-
    public interface Api
    {
        @Multipart
        @POST("frame_image_save.php")
        Call<AddPlayerMemberModel> getResult(@Part("user_id") RequestBody user_id
                , @Part("imagename") RequestBody imagename
                , @Part("type")RequestBody type
                , @Part("image") MultipartBody.Part imageFile
                , @Part("gif_image")RequestBody gifimage
        );
    }

Expected: Show the Toast Message Image Frame Successfully

Actual Result:

Caused by: java.lang.IllegalArgumentException: @Part parameters using the MultipartBody.Part must not include a part name in the annotation. (parameter #4) for method Api.getResult

Vall0n
  • 1,601
  • 2
  • 14
  • 19
Ankit Gupta
  • 139
  • 1
  • 11

2 Answers2

1

Update it....

{
    @Multipart
    @POST("frame_image_save.php")
    Call<AddPlayerMemberModel> getResult(@Part("user_id") RequestBody user_id
            , @Part("imagename") RequestBody imagename
            , @Part("type")RequestBody type
            , @Part MultipartBody.Part imageFile
            , @Part("gif_image")RequestBody gifimage
    );
}
RequestBody mRequestBody  = RequestBody.create(MediaType.parse("image/*"), file)
MultipartBody.Part fbody = MultipartBody.Part.createFormData("image", "filename.jpg", mRequestBody)
Federico Galfione
  • 1,069
  • 7
  • 19
abhi kumar
  • 24
  • 2
  • 4
  • Thank you it works, I need to understand where are we sending key here as my key is "image" and how to send a null value for the image? – Ankit Gupta Jun 11 '19 at 05:19
1

try this

private void uploadFile(Uri fileUri) {
        File file = new File(fileUri.getPath());
        long sTime = System.currentTimeMillis();
        RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part image = MultipartBody.Part.createFormData(sTime + "FramePlayer.Jpeg",file.getName(), requestFile);
        MultipartBody.Part id = MultipartBody.Part.createFormData("user_id", "1498");
        MultipartBody.Part type = MultipartBody.Part.createFormData("type", "player");
        MultipartBody.Part gif = MultipartBody.Part.createFormData("gif_image", "");
        Api apiInterface = (Api) Client.getApi();
        Call<AddPlayerMemberModel> call = apiInterface.getResult(id, image, type, gif);
        call.enqueue(new Callback<AddPlayerMemberModel>() {
            @Override
            public void onResponse(Call<AddPlayerMemberModel> call, Response<AddPlayerMemberModel> response) {
                if (!response.isSuccessful()) {
                    return;
                }
                AddPlayerMemberModel model1 = response.body();
                Toast.makeText(MainActivity.this, "Image Frame Successfully", Toast.LENGTH_SHORT).show();
                if (model1.getStatus().equals("Image Upload SuccessFully")) {

                }
            }

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

And Change like this

 @Multipart
        @POST("frame_image_save.php")
        Call<AddPlayerMemberModel> getResult(@Part MultipartBody.Part user_id,
                @Part MultipartBody.Part imageFile,
                @Part MultipartBody.Part type,
                @Part MultipartBody.Part gifimage
            );
J Ramesh
  • 548
  • 4
  • 11