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:

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

or

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

byte array to Bitmap:

InputStreaminputStream=newByteArrayInputStream(bytes);
  BitmapFactory.Optionso=newBitmapFactory.Options();
  BitmapFactory.decodeStream(inputStream, null, o);

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