Reputation: 2679
So I have a byte array which I can covert to bitmap using the below function:
private Bitmap getBitMapFromByte(byte[] imageByte) {
Bitmap bitmap = BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length);
return bitmap;
}
This produces the correct result and I can preview the returned bitmap in debug viewer.
I then have some intermediate functions that require the image to be in a byte array byte[]
. So I convert the bitmap to byte[]
using:
int byteCount = myBitmapImage.getAllocationByteCount();
ByteBuffer buffer = ByteBuffer.allocate(byteCount); //Create a new buffer
myBitmapImage.copyPixelsToBuffer(buffer); //Move the byte data to the buffer
byte[] myByte = buffer.array();
Now the same byte array converted immediately back to bitmap produces a null
Bitmap testBitmap= BitmapFactory.decodeByteArray(myByte, 0, myByte.length);
Why is a bytearray immediately converted back to bitmap returning null?
Upvotes: 0
Views: 475
Reputation: 398
To convert Bitmap to Byte Array use the following code
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
To convert Byte Array to bitmap use following code
Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, bitmapdata.length);
Upvotes: 1