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;
 }
 }
Advertisements

About krishjlk

http://www.facebook.com/krishna.kanth.121

2 responses to “How to convert bitmap to byte array in android and java”

  1. byte converter says :

    Excellent article! We are linking to this great article on
    our site. Keep up the good writing.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: