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.
Asked
Active
Viewed 348 times
-1
-
convert bitmap to base64 and upload it to server – Ganesh Gudghe Jul 10 '18 at 07:34
2 Answers
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
-
-
Read file as a string. for this visit the link https://stackoverflow.com/questions/12910503/read-file-as-string – Ankita Jul 10 '18 at 08:56
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