我正在尝试Android 9中引入的新AnimatedImageDrawable。我试图显示动画GIF,并从ByteBuffer创建源。
使用文件作为源呈现GIF效果很好。
此代码无效
ImageDecoder.createSource(ByteBuffer.wrap(file.readBytes())).also { source ->
ImageDecoder.decodeDrawable(source).also {
imageView.setImageDrawable(it)
(it as? AnimatedImageDrawable)?.start()
}
}
但这可行:
ImageDecoder.createSource(file).also { source ->
ImageDecoder.decodeDrawable(source).also {
imageView.setImageDrawable(it)
(it as? AnimatedImageDrawable)?.start()
}
}
我怀疑可能存在一些线程问题,但是我还无法弄清楚如何解决它。我尝试在Main范围的协程中调用AnimatedImageDrawable的启动函数,但是遇到相同的错误。
我希望这两种方法都可以正常工作,但是出现以下消息会导致本机崩溃:
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)
我意识到当它已经可以直接与文件一起使用时,尝试实现这一目标可能没有任何意义,但是我很好奇为什么在使用ByteBuffer时失败了。
答案 0 :(得分:1)
此崩溃是由Android framework issue引起的。您可以通过不使用ByteBuffer
来解决此问题。例如,将ByteBuffer
写入File
然后使用ImageDecoder.createSource(file)
将解决此问题。
答案 1 :(得分:0)
我有要制作动画的gif,所以我在素材资源文件夹中添加了gif,然后调用下面的方法,该方法使用协程显示图像
private fun updateImageWithImageDecoder(assetFileName: String) {
val context = requireContext()
GlobalScope.launch(Dispatchers.Default) {
val source = ImageDecoder.createSource(context.assets, assetFileName)
val drawable = ImageDecoder.decodeDrawable(source)
GlobalScope.launch(Dispatchers.Main) {
imageView.setImageDrawable(drawable)
if (drawable is AnimatedImageDrawable) {
drawable.start()
}
}
}
}