CopyPixelsFromBuffer works. But decode its ByteArray returns null

433 views Asked by At
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
bitmap.copyPixelsFromBuffer(byteBuffer);
// bitmap is valid and can be displayed

I extracted the ByteArray from the valid byteBuffer. But it returns null when I tried to decodeByteArray. Can someone explain why it's the case.

byteBuffer.rewind();
byteBuffer.get(byteArray, 0, byteBuffer.capacity());
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0 , byteArray.length);
// returns null
1

There are 1 answers

0
adelphus On

I believe the 2 functions do different things and expect different data.

copyPixelsFromBuffer()

is used to import raw pixel information into an existing Bitmap image which already has size and pixel depth configured.

BitmapFactory.decodeByteArray()

is used to create a bitmap from a byte array containing the full bitmap file data, not just the raw pixels. That's why the function doesn't take (or need) size and pixel depth information, because it gets it all from the bytes passed to it.