How to convert bitmap to byte array in android and java

 /** * @param bitmap * Bitmap object from which you want to get bytes * @return byte[] array of bytes by compressing the bitmap to PNG format <br/> * null if bitmap passed is null (or) failed to get bytes from the * bitmap */ public static byte[] convertBitmapToByteArray(Bitmap bitmap) { if (bitmap == null) { return null; } else { byte[] b = null; try { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); bitmap.compress(CompressFormat.PNG, 0, byteArrayOutputStream); b = byteArrayOutputStream.toByteArray(); } catch (Exception e) { e.printStackTrace(); } return b; } } 

Leave a comment