How To Post A Bitmap To A Server Using Retrofit/android
Solution 1:
NOTE: Make this conversion on other thread than Main. RxJava could help to achieve this, or Coroutines
First convert your bitmap to file
//create a file to write bitmap dataFilef=newFile(context.getCacheDir(), filename);
f.createNewFile();
//Convert bitmap to byte arrayBitmapbitmap= your bitmap;
ByteArrayOutputStreambos=newByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 0/*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in fileFileOutputStreamfos=null;
try {
fos = newFileOutputStream(f);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
fos.write(bitmapdata);
fos.flush();
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
After that create a request with Multipart
in order to upload your file
RequestBodyreqFile= RequestBody.create(MediaType.parse("image/*"), f);
MultipartBody.Partbody= MultipartBody.Part.createFormData("upload", f.getName(), reqFile);
Your service call should look like this
interfaceService {
@Multipart@POST("/yourEndPoint")
Call<ResponseBody> postImage(@Part MultipartBody.Part image);
}
And then just call your api
Service service = newRetrofit.Builder().baseUrl("yourBaseUrl").build().create(Service.class);
Call<okhttp3.ResponseBody> req = service.postImage(body);
req.enqueue(newCallback<ResponseBody>() {
@OverridepublicvoidonResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
// Do Something with response
}
@OverridepublicvoidonFailure(Call<ResponseBody> call, Throwable t) {
//failure message
t.printStackTrace();
}
});
Solution 2:
It is recommended to upload a bitmap/image via file. The image should be saved in device storage and from that, you should send the file in multipart. But If you have a requirement to not store the bitmap/image in storage and directly upload it via Retrofit then you can do this.
- Get the Bitmap and convert it into an array of bytes (byte[])
- Convert the byte[] in Base64 (Base64 will be a single String)
- Upload the Base64 String just like a regular string
And when you need to show the image in App or at Backend. Convert the base64 to bytes and bytes to Bitmap and display the Bitmap.
Convert Bitmap to byte[]
publicstaticbyte[] bitmapToBytes(Bitmap photo) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
return stream.toByteArray();
}
Convert byte[] into Base64 String
publicstatic String bytesToBase64(byte[] bytes) {
finalStringbase64= Base64.encodeToString(bytes, 0);
return base64;
}
Convert Base64 String to byte[]
publicstaticbyte[] base64ToBytes(String base64) {
final byte[] bytes = Base64.decode(base64, 0);
return bytes;
}
Convert byte[] to Bitmap
publicstatic Bitmap bytesToBitmap(byte[] bytes) {
finalBitmapbitmap= BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
return bitmap;
}
I have seen people upload images like this but I personally prefer to upload via file.
Solution 3:
Bitmapbmp= BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStreamstream=newByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
you can convert your bitmap into a byte array and then after post this byte array into the server else you can make one tempory file for example
Filefile=newFile(this.getCacheDir(), filename);
file directly uplaod into the server
Post a Comment for "How To Post A Bitmap To A Server Using Retrofit/android"