Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide cache image on disk on background thread

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.

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

So, how can I tell Glide to cache an image from a background thread?

EDIT: 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.

like image 968
Vasiliy Avatar asked Sep 23 '18 09:09

Vasiliy


People also ask

How do you glide cache images?

How does the Glide caching mechanism work? By default, Glide uses memory and disk caching to avoid unnecessary network calls, it checks into multiple layers of caches before initiating a new request call for an image.

What is disk cache strategy in Glide?

Disk Cache Strategies The available strategies allow you to prevent your load from using or writing to the disk cache or choose to cache only the unmodified original data backing your load, only the transformed thumbnail produced by your load, or both.


1 Answers

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
like image 173
veritas1 Avatar answered Oct 13 '22 10:10

veritas1