Glide error after activity was destroyed and restarted
I get the following error when trying to load an image from assets into ImageView after app was closed and restarted:
D/skia: --- SkImageDecoder::Factory returned null
The scenario in which this issue appears:
Android 5.0.1 (also tried on 5.1.1 - same issue)
- Start app
- Load animated image from assets into ImageView (image is loaded fine)
- Make sure activity is destroyed
- Restart activity
- Try to load same image - get the above-mentioned errors
However (!), if I don't load image in step 2, but just close and restart app, and load image in step 5, there is no error - image is loaded fine.
Here is my code for loading image:
Glide.with(applicationContext)
.using(new AssetUriLoader(applicationContext))
.load(Uri.parse(assetPath))
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(imageView);
The AssetUriLoader is a custom loader suggested by vpratfr in the following ticket:
The ImageView is created programmatically:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(mImageViewWidth, mImageViewHeight);
ImageView imageView = new ImageView(getActivity());
params.leftMargin = 100;
params.topMargin = 100;
rl.addView(imageView, params);
If someone had this error after app is restarted, and found a solution, help would be greatly appreciated.
That's a really old issue, and you used the workaround from an early comment. The issue was closed by a commit that added support for assets, and I think you should use a defacto asset uri: file:///android_asset/path/and/filename_under_assets_folder without the using call, however it may not fix the issue, but in the worst case you will have one less class in your app which is always good!
Something that's more likely gonna affect the problem is using the Activity (or better yet the Fragment if any) as the context. If you turn on logging you may see that the old GIF is still playing or something is conflicting because the resource lived through an activity death, GIFs are a weird bunch. For more on with see http://stackoverflow.com/a/32887693/253468.
When you say animated, I hope meant a GIF file? If so you should disable RESULT caching, because it really slows down loading. Also since this is already a local file (in the APK) there's no need to duplicate it on the SD card with SOURCE caching, saving the user precious bytes, so in the end you should be using NONE instead of ALL. Again this may affect the behaviour you see.
Can you please try these one by one and report back if any of them (alone or all combined) does any good to the issue?
Before you change anything, please enable logging or add a debug listener to see the root cause!
I tried to recreate this on an 5.0.0 emulator, but I wasn't able to: I turned on "Don't keep activities" in Developer options and loaded the image in onCreate, here's the log:
03-05 19:31:21.866 1756-1756/com.bumptech.glide.supportapp.v3 I/ACTIVITY: Created: null
03-05 19:31:21.981 1756-1756/com.bumptech.glide.supportapp.v3 I/ACTIVITY: Started
03-05 19:31:21.991 1756-1777/com.bumptech.glide.supportapp.v3 D/OpenGLRenderer: Render dirty regions requested: true
03-05 19:31:22.125 1756-1777/com.bumptech.glide.supportapp.v3 I/OpenGLRenderer: Initialized EGL, version 1.4
03-05 19:31:23.400 1756-1756/com.bumptech.glide.supportapp.v3 D/GLIDE: .onResourceReady(GifDrawable@372ae680(400x400), file:///android_asset/emo-yimo.gif, Target for: ImageView{167f54b9 V.ED.... ........ 100,100-700,500}(600x400->600x400), false, true)
03-05 19:31:33.123 1756-1756/com.bumptech.glide.supportapp.v3 I/ACTIVITY: Stopped
03-05 19:31:33.123 1756-1756/com.bumptech.glide.supportapp.v3 I/ACTIVITY: Destroyed
03-05 19:31:36.064 1756-1756/com.bumptech.glide.supportapp.v3 I/ACTIVITY: Created: null
03-05 19:31:36.074 1756-1756/com.bumptech.glide.supportapp.v3 D/GLIDE: .onResourceReady(GifDrawable@1d9bc41f(400x400), file:///android_asset/emo-yimo.gif, Target for: ImageView{200e236c V.ED.... ......I. 0,0-0,0}(600x400->0x0), true, true)
03-05 19:31:36.076 1756-1756/com.bumptech.glide.supportapp.v3 I/ACTIVITY: Started
As you can see from true, true the second instance was from memory cache.
From getActivity() I guess you use fragments as well, can you please try to create a small reproducible case of this?
Thanks a lot, @TWiStErRob, I will try all you suggestions and let you know how it's going.
My answers to your questions:
Yes, I am using GIFs for testing.
I will change SOURCE caching to ALL.
I use Fragment as a context. I will try to use Activity instead.
When you report an issue (in general) don't make anything in the report different to your original source code as it may contain clues to the reader: for example if you hadn't written applicationContext I would have skipped that whole paragraph because it's not relevant. The finest context is the best: Fragment wins over Activity, Activity wins over appContext. But it may worth playing around to see if it makes a difference.
I suggested to use NONE, anything else is wasteful in some way for local GIFs. I guess that was just a brainfart in your comment.
@TWiStErRob, sorry about confusion with ALL and NONE. The reason for using ALL or SOURCE is that I preload images when the Activity starts. I read somewhere on the forums your comments that after preloading images, we need to load them using ALL or SOURCE.
Right now I turned off preloading to isolate the issue, but I will need it later.
I think you mean downloadOnly, not preload. Those other comments should be all about remote images over HTTP, but your local asset will be as fast as it gets: snail mail ≫ network ≫ disk ≫ memory. You can't really preload GIFs, because it is constantly changing and only two frames are loaded at a time. Try to re-read my reasoning about SOURCE/RESULT in the first comment in this light.
@TWiStErRob, thanks for this explanation. I changed to NONE, and the images are loading instantly now.
I tried all the steps you suggested for fixing/troubleshooting the issue. What I found - even though after pressing back button onPause(), onStop and onDestroy() are called for both - Activity and Fragment, the app is not destroyed - it's still in the stack of the apps. If I kill it manually, and restart it again, then images are loaded fine.
I noticed that the app sometimes stays alive while investigating #1021. I think it's safe to ignore unless it causes problems. What did you find out about the root cause with logging?
This is the logging after I enable VERBOSE and added LoggingListener
D/IMAGE_LOADER: loading image: itemImages/giphy1.gif
V/Engine: Started new load in 0.152588ms, key: EngineKey{itemImages/giphy1.gif+com.bumptech.glide.signature.EmptySignature@f83f84d+[300x300]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+'FitCenter.com.bumptech.glide.load.resource.bitmap'+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
V/DecodeJob: Fetched data in 4.669188999999999, key: EngineKey{itemImages/giphy1.gif+com.bumptech.glide.signature.EmptySignature@f83f84d+[300x300]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+'FitCenter.com.bumptech.glide.load.resource.bitmap'+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
E/: ReadStreamToBuffer : Qmage Stream read error!! required length 12 bytes, but red 0 bytes
E/: isQmage : input SkStreamRewindable length is less than Qmage minimum size : 0
D/skia: --- SkImageDecoder::Factory returned null
E/: ReadStreamToBuffer : Qmage Stream read error!! required length 12 bytes, but red 0 bytes
E/: isQmage : input SkStreamRewindable length is less than Qmage minimum size : 0
D/skia: --- SkImageDecoder::Factory returned null
E/MediaMetadataRetrieverJNI: getFrameAtTime: videoFrame is a NULL pointer
V/DecodeJob: Decoded from source in 16.204835, key: EngineKey{itemImages/giphy1.gif+com.bumptech.glide.signature.EmptySignature@f83f84d+[300x300]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+'FitCenter.com.bumptech.glide.load.resource.bitmap'+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
V/DecodeJob: Transformed resource from source in 0.061035, key: EngineKey{itemImages/giphy1.gif+com.bumptech.glide.signature.EmptySignature@f83f84d+[300x300]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+'FitCenter.com.bumptech.glide.load.resource.bitmap'+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
V/DecodeJob: Transcoded transformed from source in 0.0, key: EngineKey{itemImages/giphy1.gif+com.bumptech.glide.signature.EmptySignature@f83f84d+[300x300]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+'FitCenter.com.bumptech.glide.load.resource.bitmap'+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+'GifBitmapWrapperDrawableTranscoder.com.bumptech.glide.load.resource.transcode'+''}
D/GenericRequest: load failed
D/GLIDE: onException(null, file:///android_asset/itemImages/giphy1.gif, Target for: android.widget.ImageView{1ebaaa32 V.ED.... ........ 219,450-519,750}, true)
Hmm, another null exception... it looks like there was some read/decode error, can you try to reproduce it with another image? Those Qmage lines I've never seen before.
Interesting - I tried two other GIFs, and they both failed with same errors even from the first load (before I press back button). Those images used to be displayed fine before. The original image still loads on the first try, but fails on second.
Can you share these gif files? Could you please make a small sample app that reproduces the issue?
Well, something happened to the files. I tried to attach them, but attachment failed with an error "The file is empty". When I tried to open them by just double-clicking on them, I got same error "File could not be opened because it's empty". I was testing the app with those files for a while.
well, that explains the "but red 0 bytes" error message...
Yes. I am wondering why they suddenly became empty.....
goto link : https://github.com/bumptech/glide/issues/1104