Skip to content Skip to sidebar Skip to footer

Convert Bitmap To ByteArray And Vice Versa

In my android application i want to convert image ,taken from camera, to byte array and convert back to bitmap to view in a image view. I can do it easily through Bitmap.compress.

Solution 1:

Bitmap to byte array:

ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] imageBytes = stream.toByteArray();

or

int bytes = bitmap.getByteCount();
ByteBuffer buffer = ByteBuffer.allocate(bytes); 
bitmap.copyPixelsToBuffer(buffer); 
byte[] array = buffer.array();

byte array to Bitmap:

 InputStream inputStream = new ByteArrayInputStream(bytes);
  BitmapFactory.Options o = new BitmapFactory.Options();
  BitmapFactory.decodeStream(inputStream, null, o);

Post a Comment for "Convert Bitmap To ByteArray And Vice Versa"