[英]I can convert a byte array to Bitmap but the same process in reverse produces a null
So I have a byte array which I can covert to bitmap using the below function:所以我有一个字节数组,我可以使用下面的 function 将其转换为 bitmap:
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.这会产生正确的结果,我可以在调试查看器中预览返回的 bitmap。
I then have some intermediate functions that require the image to be in a byte array byte[]
.然后我有一些中间函数,要求图像位于字节数组byte[]
中。 So I convert the bitmap to byte[]
using:所以我将 bitmap 转换为byte[]
使用:
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 产生 null
Bitmap testBitmap= BitmapFactory.decodeByteArray(myByte, 0, myByte.length);
Why is a bytearray immediately converted back to bitmap returning null?为什么字节数组立即转换回 bitmap 返回 null?
To convert Bitmap to Byte Array use the following code要将 Bitmap 转换为字节数组,请使用以下代码
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 bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, bitmapdata.length);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.