unknown os platform
how to convert a bitmap image into byte array in android and reverse | Whats-online.info
whats-online-info

how to convert a bitmap image into byte array in android and reverse


how
2017/02/07 / Science and Tutorials

Android tutorial; Learn how you can simply convert a bitmap image into byte array programmatically. See an example.





Convert bitmap image to byte array android without compress


This post takes you through how to convert a bitmap image into byte array in android programmatically and vice versa. This conversion might be needful especially when you want to store images into SQLite database as byte data type.



Note, this code is targeted to API  (Build.VERSION_CODES.HONEYCOMB_MR1)

Let us code.

Function to convert a bitmap image to byte array
The function above returns a byte array data type. 

 @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR1)     private byte[] profileImage(Bitmap b){         ByteArrayOutputStream bos = new ByteArrayOutputStream();         b.compress(Bitmap.CompressFormat.PNG, 0, bos);         return bos.toByteArray();     }      byte[] imageByte=profileImage(BitmapImage);

Function to convert a byte array back to bitmap image
The function above returns a bitmap image data type. 

 private Bitmap convertToBitmap(byte[] b){         return BitmapFactory.decodeByteArray(b , 0, b.length);     } Bitmap img=convertToBitmap(byteArray);

Thanks for your time. Subscribe to our newsletter to get the latest updates directly to your mailbox.













You may also like:


Leave a comment












Subscribe to get replies direct to your mailbox

Submit comment