Search code examples
javaandroidbitmapbitmapfactory

BitmapFactory.decodeByteArray returns null when decoding data from ByteBuffer.array()


I am decoding data coming from a ByteBuffer like: BitmapFactory.decodeByteArray(inputData.array(), 0, inputData.limit());

The same code works fine on older Android (4.3 for example), but on Android 7 I get the error "D/skia (14391): --- SkImageDecoder::Factory returned null" and the returned image is null.

The image data is correctly loaded from a jpg file. Also the ByteBuffer has correct position and limit.

I read most of the similar questions related to BitmapFactory.decodeByteArray, but none seems to resemble my scenario.


Solution

  • It seems that if I first read the data from the ByteBuffer into an array and then provide this data,BitmapFactory.decodeByteArray is able to decode the image correctly. I missed the offset inside the backing array where the ByteBuffer data actually starts.

    So the correct code would be:

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