2

我正在使用凌空加载我的图像并缓存它们。

mImageLoader = new ImageLoader(getRequestQueue(context), mImageCache);

其中 mImageCache 是一个 DiskLruImageCache。

volley 从服务器获取图像,通过ImageRequest它扩展ImageRequest<Bitmap>

在请求类中有一个布尔值,它定义是否缓存响应

/** Whether or not responses to this request should be cached. */
private boolean mShouldCache = true;

并且ImageRequest没有禁用mShouldCache

如您所见,默认值为 true ,因此在 volley 获取图像后,将其缓存在 volley 缓存目录下diskBasedCache

所以现在我必须缓存位图一个ImageRequest和一个ImageLoader如何禁用ImageRequest缓存?或任何其他建议?

4

1 回答 1

3

You are making a mistake giving the ImageLoader a disk cache. Volley already has a shared disk cache for every response, be it an image are not, that works according to HTTP cache headers by default.

You are supposed to provide a memory bitmap cache to the ImageLaoder. Look at the documentation.

The reasoning for it is how Volley is designed. This is the image request logic for Volley:

  1. Image with url X is added to the queue.
  2. Check image memory cache (provided by you) - If available, return bitmap. quickest
  3. Check shared disk cache - If available, check cache headers to see that image is still valid. If valid - add to memory bitmap cache and return. slower, but still pretty quick
  4. This step means that either the image was in the disk cache but its cache headers are missing or expired, or the image wasn't available in the cache at all. Either way, Volley performs a network request and caches the response in both caches. slowest

So by providing a disk cache - you are both slowing down your app and taking up to twice as much disk space with redundant image saving.

Use a memory cache.

于 2014-06-23T15:59:35.323 回答