Search code examples
cachingandroid-volleydiskcache

Disk Caching in Volley is not working


I am using Volley for caching of images in the app. The memory caching is working fine, but no image is getting cached on the disk. The code is given below

VolleySingleton.java

public class VolleySingleton {

    public static final String TAG = "VolleySingleton";

     private static VolleySingleton mInstance = null;
    private RequestQueue mRequestQueue;
    private ImageLoader mImageLoader;

    private VolleySingleton(Context context){

        mRequestQueue = Volley.newRequestQueue(context);
        mImageLoader = new ImageLoader(this.mRequestQueue,new ImageLoader.ImageCache() {

            private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(3);

            @Override
            public Bitmap getBitmap(String url) {
                return mCache.get(url);
            }

            @Override
            public void putBitmap(String url, Bitmap bitmap) {
                mCache.put(url,bitmap);
            }
        });
    }

    public static VolleySingleton getmInstance(Context context){
        if(mInstance == null){
            mInstance = new VolleySingleton(context);
        }
        return mInstance;
    }

    public RequestQueue getmRequestQueue(){
        return this.mRequestQueue;
    }

    public ImageLoader getmImageLoader(){
        return mImageLoader;
    }
}

The Images are loaded in a CustomAdapter ChannelAdapter.java

private class ChannelAdapter extends BaseAdapter{

    private LayoutInflater inflater;
    private VolleySingleton volleySingleton;

    public ChannelAdapter(Context context) {
        super(context);
        inflater = (LayoutInflater)             context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        volleySingleton = VolleySingleton.getmInstance(context);
    }


    @Override
    public View getView(int i, View view, ViewGroup viewGroup) {
        if(view == null){
            view = inflater.inflate(R.layout.item,viewGroup,false);
        }
        NetworkImageView imageView = (NetworkImageView) view.findViewById(R.id.imageView);
        TextView textView = (TextView) view.findViewById(R.id.textView);
        textView.setText(tvChannel.getName());
        imageView.setImageUrl(tvChannel.getImages().get(link,volleySingleton.getmImageLoader());

        return  view;
    }
}

Solution

  • OK, i fixed the problem. It was because the images i obtained from the server had a cache-control tag set to no-cache on its response

    Cache-Control: no-cache,no-cache,max-age=15552000

    I made volley neglect this tag by modifiying HttpHeaderParser.java in the library. Now its working fine