2017-08-02 77 views
1

我解碼數據從ByteBuffer來像:BitmapFactory.decodeByteArray(inputData.array(), 0, inputData.limit());BitmapFactory.decodeByteArray從ByteBuffer.array()進行解碼的數據時,則返回null

相同的代碼工作正常上較舊的Android(4.3例如),但在Android 7我收到錯誤"D/skia (14391): --- SkImageDecoder::Factory returned null",返回的圖像爲空。

圖像數據從jpg文件正確加載。 ByteBuffer也有正確的位置和限制。

我讀了大部分與BitmapFactory.decodeByteArray相關的類似問題,但似乎沒有一個類似於我的場景。

回答

0

看來,如果我第一次從ByteBuffer讀取數據到數組中,然後提供這些數據,BitmapFactory.decodeByteArray就能夠正確解碼圖像。我錯過了ByteBuffer數據實際啓動的backing數組內的偏移量。

所以正確的代碼是:

Bitmap im = BitmapFactory.decodeByteArray(inputData.array(), inputData.arrayOffset(), inputData.limit());