Search code examples
androiduniversal-image-loader

How can I use Already downloaded images from cache with Universal Image Loader


I used Universal Image Loader to fetch the Data from uri in let say Activity A, but I don't to use the displayImage() method to fetch the images from uri again at Activity B.

Instead I want to fetch the images already stored in cache to another Activity B

How can this be done?

I have used these options to initialize the Image Loader

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
                .cacheOnDisc(true).cacheInMemory(true)
                .imageScaleType(ImageScaleType.EXACTLY)
                .displayer(new FadeInBitmapDisplayer(300)).build();

ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
                getApplicationContext())
                .defaultDisplayImageOptions(defaultOptions)
                .memoryCache(new WeakMemoryCache())
                .discCacheSize(100 * 1024 * 1024).build();

ImageLoader.getInstance().init(config);

and then called this to get the Image

ImageLoader imageLoader = ImageLoader.getInstance();

//download and display image from url
imageLoader.displayImage(url, myImageView);

Solution

  • Solution to :

    Yes it's working in Activity B, but it is making unwanted connections to download the image when the internet is not connected, Every time the 'getView()' method is called in ListView when scrolling. 'failed to connect to /192.168.1.3 (port 80) after 5000ms: connect failed: ENETUNREACH (Network is unreachable) java.net.ConnectException: failed to connect to /192.168.1.3 (port 80) after 5000ms: connect failed: ENETUNREACH (Network is unreachable)`

      ImageLoader imageLoader = ImageLoader.getInstance();
    
            File file = imageLoader.getDiskCache().get(image_url);
            if (file==null) {
    
                //Load image from network
            }
            else {
               //Load image from cache
                mImageView.setImageURI(Uri.parse(file.getAbsolutePath()));
            }