Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Glide, How to display image cache while charging the request?

When the internet is slow, Glide takes too long time to show the image, I need to display the image immediately.

like image 902
Celggar Avatar asked Dec 28 '25 16:12

Celggar


1 Answers

1) If your image is not changing rapidly you could use Glide disk and memory caching feature. Define the strategy using .diskCacheStrategy(DiskCacheStrategy) method. There are several DiskCacheStrategy you could use:

  • DiskCacheStrategy.NONE caches nothing
  • DiskCacheStrategy.SOURCE caches only the original full-resolution image.
  • DiskCacheStrategy.RESULT caches only the final image, after reducing the resolution
  • DiskCacheStrategy.ALL caches all versions of the image. This is the default behavior

2) If your image changes rapidly you'll need to maybe load the cached image right away and then do the network call to fetch the update and load it if success. There are ways to do this but maybe you wanna look at RxJava and .concat

like image 101
Aitor Viana Avatar answered Dec 30 '25 06:12

Aitor Viana