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.
2 Answers
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.
2 Comments
Sudip Sadhukhan
FileOutputStream require string values
Ankita
Read file as a string. for this visit the link stackoverflow.com/questions/12910503/read-file-as-string
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); }