从2016年开始,我们小组在项目中就开始使用了Glide,目前使用的版本是3.7.0 。但是最近自测发现了一个不起眼的问题,因为这个问题又发现了别的问题,好多坑啊!!
第一个问题,发现已经用Glide加载好了的图片,再次显示该图时,Glide竟然又从网络下载这张图片。。。
观察现象:加载完了所有图片墙里面的缩略图后,不断尝试从底部滑动到底部,然后从底部滑动到顶部,就会发现某张或者某些图片加载比较慢,这是一个可复现的问题,应该好办!
问题跟踪:首先检查下自己的代码,是否有重复设置默认图的代码存在?检查发现自己的代码里面最后都是调用Glide来加载的,那就对Glide进行日志调试。如何打开Glide的日志调试呢?自己网上搜索一下就有很多,这里简单介绍下:
比如我想打开Glide加载网络数据和本地缓存数据的相关日志,这部分代码在DecodeJob中,那就使用命令打开这部分的日志
adb shell setprop log.tag.DecodeJob VERBOSE
一般来说,比较重要的日志包括Engine、EngineRunnable、DecodeJob;打开这些日志就够我定位这个问题了。
观察了Glide的日志输出,发现Glide在加载某张已经加载过的图片的时候,在Engine中又开启了一个新的任务,输出了日志:Started new load。那就是说Glide并没有命中缓存,接下来我就仔细观察了DecodeJob的日志输出,发现了一些端倪:
第一次缓存图片时候的Log(从网络加载图片)
03-14 14:21:22.584 26188-26540/? I/CusHttpUrlFetcher: star to load image
03-14 14:21:22.592 26188-26540/? D/CusHttpUrlFetcher: image url: http://xxxxxxx/
03-14 14:21:23.673 26188-26540/? V/DecodeJob: Fetched data in 1081.081359, key: EngineKey{http://xxxxxxx/+com.bumptech.glide.signature.EmptySignature@2e6bc8b8+[221x148]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+''+''}
03-14 14:21:23.653 26188-26540/? V/DecodeJob: Decoded from source in 20.341332996989517, key: EngineKey{http://xxxxxxx/+com.bumptech.glide.signature.EmptySignature@2e6bc8b8+[221x148]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+''+''}
03-14 14:21:23.653 26188-26540/? V/DecodeJob: Transformed resource from source in 0.036178, key: EngineKey{http://xxxxxxx/+com.bumptech.glide.signature.EmptySignature@2e6bc8b8+[221x148]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+''+''}
03-14 14:21:23.670 26188-26540/? V/DecodeJob: Wrote transformed from source to cache in 14.342681, key: EngineKey{http://xxxxxxx/+com.bumptech.glide.signature.EmptySignature@2e6bc8b8+[221x148]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+''+''}
03-14 14:21:23.670 26188-26540/? V/DecodeJob: Transcoded transformed from source in 0.049123, key: EngineKey{http://xxxxxxx/+com.bumptech.glide.signature.EmptySignature@2e6bc8b8+[221x148]+''+'ImageVideoBitmapDecoder.com.bumptech.glide.load.resource.bitmap'+''+'BitmapEncoder.com.bumptech.glide.load.resource.bitmap'+''+''}
第二次没命中缓存,又去加载网络图片的log
03-14 14:58:25.558 26188-26188/? V/Engine: Started new load in 0.21012499999999998ms, key: EngineKey{http://xxxxxxx/+com.bumptech.glide.signature.EmptySignature@2e6bc8b