4

I'm trying to call .evictAll() on the cache of my ImageLoader, I can't figure out how to access the method

private VolleySingleton(){
        mRequestQueue = Volley.newRequestQueue(VolleyApplication.getAppContext());

        mImageLoader = new ImageLoader(this.mRequestQueue, new ImageLoader.ImageCache() {
            private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
            public void flushLruCache(){ mCache.evictAll();};
            public void putBitmap(String url, Bitmap bitmap) {
                mCache.put(url, bitmap);
            }
            public Bitmap getBitmap(String url) {
                return mCache.get(url);
            }
        });

    }

 mRequestQueue = VolleySingleton.getInstance().getRequestQueue();
 mImageLoader = VolleySingleton.getInstance().getImageLoader();

I've tried casting my mImageLoader object

((ImageLoader.ImageCache) mImageLoader).flushLruCache();

But that throws an error saying I can't cast those types.

How do access the .flushLruCache() method ?

KamaL
  • 37
  • 8
JTK
  • 1,469
  • 2
  • 22
  • 39

1 Answers1

2

If I have not misunderstood, you can keep a reference to the ImageLoader.ImageCache, in your class

private ImageLoader.ImageCache mImageCache;    
private VolleySingleton(){
    mRequestQueue =  Volley.newRequestQueue(VolleyApplication.getAppContext());
    mImageLoader = new ImageLoader(this.mRequestQueue,  mImageCache = new ImageLoader.ImageCache() {
        private final LruCache<String, Bitmap> mCache = new LruCache<String, Bitmap>(10);
        public void flushLruCache(){ mCache.evictAll();};
        public void putBitmap(String url, Bitmap bitmap) {
            mCache.put(url, bitmap);
        }
        public Bitmap getBitmap(String url) {
            return mCache.get(url);
        }


    });
} 

and declare an evictAllImages in it

 public void evictAllImages() {
      if (mImageCache != null) {
           mImageCache.flushLruCache();
      }
 }
Blackbelt
  • 156,034
  • 29
  • 297
  • 305
  • That worked perfectly, I had to add flushLruCache(); to the ImageCache Interface in ImageLoader.java for this to work, Thank you. – JTK Apr 14 '15 at 20:31