Search code examples
androidandroid-glide

How to check is Image is already cached in android mobile using Glide


I am storing my image and its reduced size image (blurred) in my Amazon Server, and store both path in database.

Now I want to know how to show blurred image first if original image is not cached and on clicking download it will download original Image. I am using Glide here...

I tried this

Glide.with(this)
           .load(mainUrl)
           .diskCacheStrategy(DiskCacheStrategy.SOURCE)
           .thumbnail(Glide.with(this)
                .load(url)                                            
                .diskCacheStrategy(DiskCacheStrategy.SOURCE))
           .centerCrop()
           .into(imageView);

but problem is It automatically download original image in background.


Solution

  •     Glide.with(TheActivity.this)
       .load("http://sampleurl.com/sample.gif")
       .diskCacheStrategy(DiskCacheStrategy.SOURCE)
       .into(theImageView);
    

    Your code will prevent Glide from downloading the GIF and will only show the GIF if it is already cached, which it sounds like you don't want.

    • Yes, the old image will eventually be removed. By default Glide uses an LRU cache, so when the cache is full, the least recently used image will be removed. You can easily customize the size of the cache to help this along if you want. See the Configuration wiki page for how to change the cache size.
    • Unfortunately there isn't any way to influence the contents of the cache directly. You cannot either remove an item explicitly, or force one to be kept. In practice with an appropriate disk cache size you usually don't need to worry about doing either. If you display your image often enough, it won't be evicted. If you try to cache additional items and run out of space in the cache, older items will be evicted automatically to make space.