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?
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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.