2

i'm using volley to load my images and cache them.

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

which mImageCache is a DiskLruImageCache.

volley fetches images from server by ImageRequest which extend the ImageRequest<Bitmap>

and in request class there is boolean that defines whether to cache the response or not

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

and ImageRequest hasn't disabled mShouldCache.

as you can see the default value is true so after volley fetches an image caches it under the volley cache directory by diskBasedCache.

so now i have to cache bitmap one from ImageRequest and one from ImageLoader how can i disable ImageRequest cache ? or any other suggestions ?

EC84B4
  • 7,676
  • 4
  • 23
  • 34
  • I think you have to use AndroidQuery lib for load image from server url which provide various way to handle cached images from server :https://code.google.com/p/android-query/ – Haresh Chhelana Jun 23 '14 at 06:49
  • thanks for library but i wanna stick with volley – EC84B4 Jun 23 '14 at 06:55

1 Answers1

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.

Itai Hanski
  • 8,540
  • 5
  • 45
  • 65
  • but i need a DiskLruCache ! i sovled this problem by creating a new instance of requestQueue by myself and passed the NoCache as cache and used my own image cache ! – EC84B4 Jun 24 '14 at 17:11
  • You don't understand the code you are you using, and probably didn't understand my answer. If you want the disk cache, edit the Volley source and change the disk cache. The `ImageLoader` expects a memory cache. If you give it a disk cache, you are hurting your app, and the user by using redundant disk space. – Itai Hanski Jun 24 '14 at 23:43
  • but volleys diskBasedCache caches entities based on httpHeader and i want the images to be LRU cache ! is there any workaround ? – EC84B4 Jun 25 '14 at 10:21
  • Edit the source. It's OK. That's why it's open. – Itai Hanski Jun 25 '14 at 10:58