androidcachingandroid-glide

Remove image from cache in Glide library


I am using Glide in one of my projects to show images from files.

Below is my code of 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 the cache for new images at that location.

If I change the image at location imagePath with some other image having the same name then the Glide is showing the first image instead of the 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 the image from the cache before getting the newly replaced image? This will also solve the problem.


Solution

  • This is how I solved this problem.

    Method 1: When the URL changes whenever the 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 the skipMemoryCache() method.

    Method 2: When the URL doesn't change, however, the 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.