8

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.

Rahul Tiwari
  • 6,851
  • 3
  • 49
  • 78
  • Are you sure that is the case? Bitmaps should be in memory after loading images using Glide, considering it has a built-in `BitmapPool` where it fetches the same bitmaps with the same hash if called from anywhere else. Calling the same URL should fetch the bitmap from the bitmap pool, not from disk cache. – Furkan Yurdakul Oct 13 '20 at 10:58
  • @FurkanYurdakul my observation is that after preloading an image if I use it, it shows an empty imageview for fraction of a second; which is not the case for an image that was already loaded in any view. – Rahul Tiwari Oct 14 '20 at 16:57
  • That might be because Glide may have been trying to load the bitmap itself from a background thread and then putting the bitmap to the target at main thread. You might experience a slight delay but it shouldn't create huge problems. If you want, you can add a callback to every request (a listener I mean), cache it yourself and directly load it from there at main thread if the request is the same. – Furkan Yurdakul Oct 15 '20 at 06:07

3 Answers3

1

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

onlyRetrieveFromCache( true )

to load the image only from memory

Mohammad Sommakia
  • 1,773
  • 3
  • 15
  • 48
1

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.

A. Kazarovets
  • 1,607
  • 1
  • 12
  • 10
1

Preload with Glide v4.6.1

RequestOptions requestOptions = RequestOptions
        .diskCacheStrategy(DiskCacheStrategy.ALL);

Glide.with(appContext)
        .asBitmap()
        .load(model)
        .apply(requestOptions)
        .submit();
yoAlex5
  • 29,217
  • 8
  • 193
  • 205