Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove image from cache in Glide library

I am using Glide in one of my projects to show image from file.

Below is my code how I am showing the image:

Glide.with(DemoActivity.this)      .load(Uri.parse("file://" + imagePath))      .into(mImage); 

The image at this location(imagePath) keeps on changing. By default Glide cache the image it shows in the ImageView. Because of this, the Glide was showing the first image from cache for new images at that location.

If I change the image at location imagePath with some other image having same name then the Glide is showing the first image instead of new one.

Two queries are:

  1. Is it possible to always the image from File and not cache? This way problem will be solved.

  2. Is it possible to clear image from cache before getting newly replaced image? This will also solve the problem.

like image 811
Nitesh Kumar Avatar asked Oct 30 '15 19:10

Nitesh Kumar


People also ask

Does glide cache images by default?

By default, Glide uses memory and disk caching to avoid unnecessary network calls, it checks into multiple layers of caches before initiating a new request call for an image.

Is glide better than Picasso?

Glide's loading times are faster and it uses a small amount of memory for cache, but the library size is quite large. It, too, is easy to implement. Glide might be a better alternative to Picasso when memory footprint is less of a concern or more and larger images need to be processed.

How do I reduce the size of a photo in Glide?

Option 1: Resizing image size with override(x, y) . centerCrop() is a cropping technique that scales the image so that it fills the requested bounds of the ImageView and then crops the extra. The ImageView will be filled completely, but the entire image might not be displayed.


1 Answers

This is how I solved this problem.

Method 1: When the URL changes whenever image changes

Glide.with(DemoActivity.this)     .load(Uri.parse("file://" + imagePath))     .diskCacheStrategy(DiskCacheStrategy.NONE)     .skipMemoryCache(true)     .into(mImage); 

diskCacheStrategy() can be used to handle the disk cache and you can skip the memory cache using skipMemoryCache() method.

Method 2: When URL doesn't change, however, image changes

If your URL remains constant then you need to use Signature for image cache.

Glide.with(yourFragment)      .load(yourFileDataModel)      .signature(new StringSignature(yourVersionMetadata))      .into(yourImageView); 

Glide signature() offers you the capability to mix additional data with the cache key.

  • You can use MediaStoreSignature if you are fetching content from media store. MediaStoreSignature allows you to mix the date modified time, mime type, and orientation of a media store item into the cache key. These three attributes reliably catch edits and updates allowing you to cache media store thumbs.
  • You may StringSignature as well for content saved as Files to mix the file date modified time.
like image 64
Nitesh Kumar Avatar answered Oct 06 '22 13:10

Nitesh Kumar