繁体   English   中英

我可以将字节数组转换为 Bitmap 但反向相同的过程会产生 null

[英]I can convert a byte array to Bitmap but the same process in reverse produces a null

所以我有一个字节数组,我可以使用下面的 function 将其转换为 bitmap:

private Bitmap getBitMapFromByte(byte[] imageByte) {
        Bitmap bitmap = BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length);
        return bitmap;
    }

这会产生正确的结果,我可以在调试查看器中预览返回的 bitmap。

然后我有一些中间函数,要求图像位于字节数组byte[]中。 所以我将 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();

现在相同的字节数组立即转换回 bitmap 产生 null

Bitmap testBitmap= BitmapFactory.decodeByteArray(myByte, 0, myByte.length);

为什么字节数组立即转换回 bitmap 返回 null?

要将 Bitmap 转换为字节数组,请使用以下代码

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();

要将字节数组转换为 bitmap 使用以下代码

Bitmap bitmap = BitmapFactory.decodeByteArray(imageBytes, 0, bitmapdata.length);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM