Converting bitmap to byteArray android

You can use copyPixelsToBuffer() to move the pixel data to a Buffer, or you can use getPixels() and then convert the integers to bytes with bit-shifting.

copyPixelsToBuffer() is probably what you’ll want to use, so here is an example on how you can use it:

//b is the Bitmap

//calculate how many bytes our image consists of.
int bytes = b.getByteCount();
//or we can calculate bytes this way. Use a different value than 4 if you don't use 32bit images.
//int bytes = b.getWidth()*b.getHeight()*4; 

ByteBuffer buffer = ByteBuffer.allocate(bytes); //Create a new buffer
b.copyPixelsToBuffer(buffer); //Move the byte data to the buffer

byte[] array = buffer.array(); //Get the underlying array containing the data.

Leave a Comment