BitmapFactory.decodeByteArray returns 解码来自 ByteBuffer.array() 的数据时为空
BitmapFactory.decodeByteArray returns null when decoding data from ByteBuffer.array()
我正在解码来自 ByteBuffer 的数据,例如:BitmapFactory.decodeByteArray(inputData.array(), 0, inputData.limit());
相同的代码在旧 Android(例如 4.3)上运行良好,但在 Android 7 上我收到错误 "D/skia (14391): --- SkImageDecoder::Factory returned null"
并且返回的图像为空。
图像数据已从 jpg 文件正确加载。 ByteBuffer 也有正确的位置和限制。
我阅读了大多数与 BitmapFactory.decodeByteArray 相关的类似问题,但 none 似乎与我的情况相似。
看来,如果我先将数据从 ByteBuffer 读取到一个数组中,然后提供这些数据,BitmapFactory.decodeByteArray
就能够正确解码图像。我错过了 ByteBuffer 数据实际开始的支持数组内的偏移量。
所以正确的代码是:
Bitmap im = BitmapFactory.decodeByteArray(inputData.array(), inputData.arrayOffset(), inputData.limit());
我正在解码来自 ByteBuffer 的数据,例如:BitmapFactory.decodeByteArray(inputData.array(), 0, inputData.limit());
相同的代码在旧 Android(例如 4.3)上运行良好,但在 Android 7 上我收到错误 "D/skia (14391): --- SkImageDecoder::Factory returned null"
并且返回的图像为空。
图像数据已从 jpg 文件正确加载。 ByteBuffer 也有正确的位置和限制。
我阅读了大多数与 BitmapFactory.decodeByteArray 相关的类似问题,但 none 似乎与我的情况相似。
看来,如果我先将数据从 ByteBuffer 读取到一个数组中,然后提供这些数据,BitmapFactory.decodeByteArray
就能够正确解码图像。我错过了 ByteBuffer 数据实际开始的支持数组内的偏移量。
所以正确的代码是:
Bitmap im = BitmapFactory.decodeByteArray(inputData.array(), inputData.arrayOffset(), inputData.limit());