-1

I make canvas view which I want to send in server using retrofit library. I get canvas view in bitmap format. Now I want to convert bitmap to file format so that I can upload in server in file format.

Sudip Sadhukhan
  • 1,784
  • 12
  • 21

2 Answers2

0
File file = new File(context.getCacheDir(), filename);
file.createNewFile();

/* Convert bitmap to byte array */
Bitmap bitmap = bitmap; //bitmap is your bitmap file which you want to convert
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 , bos);
byte[] bitmapdata = bos.toByteArray();

/* write the bytes in file */
FileOutputStream fos = new FileOutputStream(file);
fos.write(bitmapdata);
fos.flush();
fos.close();

Always close and flush the FileOutputStream.

Ankita
  • 1,129
  • 1
  • 8
  • 15
0

Convert ua bitmap to base64 String like below and send it using string

 public static String bitmapToBase64(Bitmap image, Bitmap.CompressFormat  compressFormat, int quality)//u can pass 100 in quality or any integer

{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}
Syed Danish Haider
  • 1,334
  • 11
  • 15