【问题标题】:Is there a way to force Glide to load images only from cache and never from network?有没有办法强制 Glide 只从缓存加载图像,而不是从网络加载图像?
【发布时间】:2020-08-03 17:02:11
【问题描述】:
在我的通话应用中,我有带有头像的通讯录。这些图片用于不同的屏幕以及来电通知。由于通知的 RemoteViews 在出现在屏幕上之前必须与 Bitmap 一起提供,并且相应的 http 请求可能需要相当长的时间,所以我想仅当它在缓存中可用时才将图像加载到通知的 RemoteViews。
有没有办法强制 Glide 只从缓存中加载图像而从不从网络中加载?
如果可以在通知出现后加载和渲染图像,我也会感谢这样的解决方案。
【问题讨论】:
标签:
android
image
push-notification
android-glide
android-remoteview
【解决方案1】:
在documentation找到路:
仅从缓存加载
在某些情况下,如果图像尚未在缓存中,您可能希望加载失败。为此,您可以针对每个请求使用 onlyRetrieveFromCache 方法:
Glide.with(fragment)
.load(url)
.onlyRetrieveFromCache(true)
.into(imageView);
如果在内存缓存或磁盘缓存中找到图像,则会加载它。否则,如果将此选项设置为 true,则加载将失败。