从Glide库中的缓存中删除图像

问题描述:

我正在我的一个项目中使用Glide来显示文件中的图像.

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);

此位置(imagePath)上的图像一直在变化.默认情况下,Glide缓存ImageView中显示的图像.因此,Glide正在为该位置的新图像显示缓存中的第一张图像.

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.

如果我将位置imagePath处的图像更改为其他具有相同名称的图像,则滑动"显示的是第一张图像,而不是新的图像.

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.

两个查询是:

  1. 是否可以始终从文件中获取图像而不缓存?这样就可以解决问题.

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

是否可以在获取新替换的图像之前从缓存中清除图像?这也可以解决问题.

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

这就是我解决此问题的方式.

This is how I solved this problem.

方法1:只要图片更改,URL就会更改

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

diskCacheStrategy()可用于处理磁盘缓存,并且您可以使用skipMemoryCache()方法跳过内存缓存.

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

方法2:但是,URL不变时,图像也会改变

如果您的URL保持不变,则需要使用签名进行图像缓存.

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()为您提供了将其他数据与缓存键混合的功能.

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

  • 如果要从媒体存储中获取内容,则可以使用MediaStoreSignature. MediaStoreSignature允许您将日期修改时间,mime类型和媒体存储项目的方向混合到缓存键中.这三个属性可靠地捕获了编辑和更新,使您可以缓存媒体存储库.
  • 对于另存为文件"的内容,您也可以StringSignature,以混合文件日期修改时间.
  • 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.