Search code examples
javaandroidcachinguniversal-image-loader

Call method on subclass of imageLoader


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 ?


Solution

  • 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();
          }
     }