Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 569
Sandeep Mishra Avatar asked Dec 19 '25 00:12

Sandeep Mishra


2 Answers

I asked in Glide Github. https://github.com/bumptech/glide/issues/2051

So add apply(RequestOptions.onlyRetrieveFromCache()) to your RequestOptions. You can register a listener and onFailed gets called when no image is in the cache.

like image 114
Arst Avatar answered Dec 20 '25 14:12

Arst


    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.
like image 36
Chirag.T Avatar answered Dec 20 '25 13:12

Chirag.T