Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide: Preload images in memory cache (with or without disk cache)

I am using

glide.load(url)
     .diskCacheStrategy(DiskCacheStrategy.ALL)    
     .preload()

to preload images.

However, I need them to be in memory and not just on disk, so it's loaded in ImageView more quickly, the way it does when I revisit the images after loading them in ImageView once.

I have also tried

  glide.load(url)
       .diskCacheStrategy(DiskCacheStrategy.ALL)
       .into(PreloadTarget.obtain(glide, PreloadTarget.SIZE_ORIGINAL, PreloadTarget.SIZE_ORIGINAL))

without much luck.

PS: I have visited this question and others, answers are outdated hence this question.

like image 333
Rahul Tiwari Avatar asked Oct 01 '20 17:10

Rahul Tiwari


People also ask

Does glide cache images by default?

Glide will put all image resources into the memory cache by default.

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.


2 Answers

Referring to this article you can cache the image in the memory then use

onlyRetrieveFromCache( true )

to load the image only from memory

like image 185
Mohammad Sommakia Avatar answered Oct 29 '22 02:10

Mohammad Sommakia


In order to get cached resource from the memory cache, loaded objects should be exactly equal. Read in docs here. To verify if your preloaded object and object loaded to ImageView are the same enable Glide logs for the Engine class.

adb shell setprop log.tag.Engine VERBOSE

Then compare the EngineKey objects that you see in logs for preload and for actual load to ImageView. They should not only have the same url and signature, but also transformations and options.

In case you load into ImageView, to not add any transformation based on ImageView params, use the

RequestOptions().dontTransform()

for the load to ImageView request.

like image 28
A. Kazarovets Avatar answered Oct 29 '22 01:10

A. Kazarovets