Universal Image Loader: Can I use cache but also refresh it?

孤人 提交于 2019-11-29 02:13:49
Vituel

So in the end I used an ImageLoadingListener as follows:

onLoadingStarted: Check for cache when loading starts.

onLoadingComplete: If no cache was found then do nothing. The request will be sent to network and cache will be updated naturally. Otherwise clear cache and call displayImage again (no listener needed this time). The cached image will be shown in the view normally. Moreover, when the 2nd loading finishes, view and cache will be updated.

ImageLoader.getInstance().displayImage(imageUri, view, new SimpleImageLoadingListener() {
            boolean cacheFound;

            @Override
            public void onLoadingStarted(String url, View view) {
                List<String> memCache = MemoryCacheUtils.findCacheKeysForImageUri(url, ImageLoader.getInstance().getMemoryCache());
                cacheFound = !memCache.isEmpty();
                if (!cacheFound) {
                    File discCache = DiscCacheUtils.findInCache(url, ImageLoader.getInstance().getDiscCache());
                    if (discCache != null) {
                        cacheFound = discCache.exists();
                    }
                }
            }

            @Override
            public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                if (cacheFound) {
                    MemoryCacheUtils.removeFromCache(imageUri, ImageLoader.getInstance().getMemoryCache());
                    DiscCacheUtils.removeFromCache(imageUri, ImageLoader.getInstance().getDiscCache());    
                    ImageLoader.getInstance().displayImage(imageUri, (ImageView) view);
                }
            }
        });
    }
KennyC

You can use the ImageLoadingListener. This interface has 4 methods to override: onLoadingStarted,onLoadingFailed,onLoadingComplete,onLoadingCancelled. In onLoadingStarted you can make the image the cached one, then on completed you change it.

So the call would look like this:

imgLoader.displayImage(url, myImageView,new ImageLoadingListener() 
{

        @Override
        public void onLoadingStarted(String arg0, View arg1) {
            //Display cached image if it exists

        }

        @Override
        public void onLoadingFailed(String arg0, View arg1, FailReason arg2) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLoadingComplete(String arg0, View arg1, Bitmap arg2)
        {

            ((ImageView)arg1).setBitmapImage(arg2);
        }

        @Override
        public void onLoadingCancelled(String arg0, View arg1) {
            // TODO Auto-generated method stub

        }
});
Shraddha Patel
private lateinit var options: DisplayImageOptions

options = DisplayImageOptions.Builder()

                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .cacheOnDisk(true)
                .cacheInMemory(true)
                .build()   

ImageLoader.getInstance().displayImage(Model.img_url, itemView.ivPlayer, options)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!