Bitmap to MultiPartEntity using retrofit

5k views Asked by At

Before i will start i see a lot of questions about this but nothing works for me maybe someone can explained or display it to me how can compress bitmap to MultiPart entity and than send it to the server correct using Retrofit

2

There are 2 answers

0
Piwo On

First you create a ResponseBody of the file and parse it as a MultipartBody.Part:

// build request containing file
RequestBody fileBody = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", "your_bitmap_file.bmp", fileBody);

This filePart can then be passed to your Retrofit Service, which should look like this:

@Multipart
@POST("/")
Call<ResponseBody> upload(@Part MultipartBody.Part file);
0
Monster Brain On

For those who come here for solution, here's mine.

First create a temporary file based on that bitmap

Convert Bitmap to file in Android (Stack Overflow)

Then get that file and add as multipart file

RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), file);
MultipartBody.Part fileBody = MultipartBody.Part.createFormData("imageFile", file.getName(), requestFile);