滑动后台线程在磁盘上的缓存图像 [英] Glide cache image on disk on background thread
问题描述
我想使用Glide急切下载图像并将其缓存在磁盘上以备将来使用.我想从后台线程调用此功能.
I want to use Glide to eagerly download images and cache them on disk for a future use. I want to call this functionality from a background thread.
我已阅读 Glide的缓存文档,但没有解释如何在没有实际目标的情况下下载图像.然后,我发现了此问题,并尝试使用类似的方法,但是无论如何我都能得到例外情况:
I've read Glide's caching documentation, but it doesn't explain how to download the image without having an actual target right now. Then I found this issue and tried to use a similar approach, but whatever I try I get this exception:
java.lang.IllegalArgumentException: You must call this method on the main thread
那么,如何告诉Glide从后台线程缓存图像?
So, how can I tell Glide to cache an image from a background thread?
编辑:我真的很想在后台线程上调用Glide的方法.我知道我可以使用Handler和其他方式将其卸载到UI线程,但这不是我要的.
I really want to call Glide's methods on background thread. I know that I can use Handler and other ways to offload it to UI thread, but that's not what I'm asking.
推荐答案
GlideApp.with(context)
.downloadOnly()
.diskCacheStrategy(DiskCacheStrategy.DATA) // Cache resource before it's decoded
.load(url)
.submit(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
.get() // Called on background thread
这篇关于滑动后台线程在磁盘上的缓存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!