Search code examples
androidandroid-drawableandroid-graphics

How to use AnimatedImageDrawable for showing animated GIFs with a ByteBuffer as source


I am experimenting with the new AnimatedImageDrawable that was introduced in Android 9. I am trying to show an animated GIF, creating the source from a ByteBuffer.

Rendering the GIF using a file as the source works fine.

This code does not work

ImageDecoder.createSource(ByteBuffer.wrap(file.readBytes())).also { source ->
                ImageDecoder.decodeDrawable(source).also {
                        imageView.setImageDrawable(it)
                        (it as? AnimatedImageDrawable)?.start()
                }
            }

However this works:

 ImageDecoder.createSource(file).also { source ->
            ImageDecoder.decodeDrawable(source).also {
                imageView.setImageDrawable(it)
                (it as? AnimatedImageDrawable)?.start()
            }
        }

I suspect there may be some threading issues going on, but I haven't been able to figure out how to solve it. I tried calling the start function of the AnimatedImageDrawable in a Coroutine in the Main scope, but I get the same error.

I expected both methods to work the same, however I get a native crash with the following message(s):

 A/libc: Fatal signal 6 (SIGABRT), code -6 (SI_TKILL) in tid 4499 (AnimatedImageTh), pid 4453 
2019-07-11 15:18:00.156 4504-4504/? A/DEBUG: Abort message: 'Failed to get JNIEnv for JavaVM: 0x756e71a400'
2019-07-11 15:18:00.212 4504-4504/? A/DEBUG: backtrace:
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #00 pc 0000000000021c9c  /system/lib64/libc.so (abort+112)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #01 pc 00000000000080dc  /system/lib64/liblog.so (__android_log_assert+312)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #02 pc 00000000001552b0  /system/lib64/libandroid_runtime.so (android::get_env_or_die(_JavaVM*)+116)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #03 pc 000000000013fe58  /system/lib64/libandroid_runtime.so (ByteArrayStream::read(void*, unsigned long)+48)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #04 pc 000000000017b498  /system/lib64/libhwui.so (SkStreamBuffer::getDataAtPosition(unsigned long, unsigned long)+244)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #05 pc 000000000043a8d0  /system/lib64/libhwui.so (SkGIFColorMap::buildTable(SkStreamBuffer*, SkColorType, int) const+180)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #06 pc 000000000016ca9c  /system/lib64/libhwui.so (SkGifCodec::initializeColorTable(SkImageInfo const&, int)+68)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #07 pc 000000000016ce30  /system/lib64/libhwui.so (SkGifCodec::prepareToDecode(SkImageInfo const&, SkCodec::Options const&)+436)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #08 pc 000000000016d070  /system/lib64/libhwui.so (SkGifCodec::onGetPixels(SkImageInfo const&, void*, unsigned long, SkCodec::Options const&, int*)+48)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #09 pc 000000000016aebc  /system/lib64/libhwui.so (SkCodec::getPixels(SkImageInfo const&, void*, unsigned long, SkCodec::Options const*)+592)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #10 pc 0000000000162b30  /system/lib64/libhwui.so (SkAnimatedImage::decodeNextFrame()+1068)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #11 pc 00000000000ec234  /system/lib64/libhwui.so (android::AnimatedImageDrawable::decodeNextFrame()+40)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #12 pc 00000000000ee9a0  /system/lib64/libhwui.so (std::__1::packaged_task<android::AnimatedImageDrawable::Snapshot ()>::operator()()+88)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #13 pc 000000000043f664  /system/lib64/libhwui.so (android::uirenderer::WorkQueue::process()+168)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #14 pc 000000000047feb4  /system/lib64/libhwui.so (android::uirenderer::ThreadBase::threadLoop()+176)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #15 pc 000000000000f9f4  /system/lib64/libutils.so (android::Thread::_threadLoop(void*)+264)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #16 pc 00000000000847b8  /system/lib64/libc.so (__pthread_start(void*)+36)
2019-07-11 15:18:00.213 4504-4504/? A/DEBUG:     #17 pc 0000000000023574  /system/lib64/libc.so (__start_thread+68)

I realise there may be no point trying to achieve this when it already works with the file directly, but I am curious as to why this fails when using the ByteBuffer.


Solution

  • This crash is caused by an Android framework issue. You can work around the issue by not using a ByteBuffer. For instance, writing the ByteBuffer to a File then using ImageDecoder.createSource(file) will work around the issue.