Search code examples
androiduniversal-image-loaderimage-caching

ImageLoader - Don't download image if it's already being cached


I don't want to download images if they are already cached. I am using ImageLoader library by NOSTRA. Please tell me if there is any way to do that. Following is the code:-

    DisplayImageOptions options = new DisplayImageOptions.Builder()
                        .showStubImage(R.drawable.ic_stub)
                        .showImageForEmptyUri(R.drawable.ic_sample)
                        .resetViewBeforeLoading()
                        .cacheInMemory()
                        .cacheOnDisc()
                        .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
                        .bitmapConfig(Bitmap.Config.ARGB_8888)
                        .build();
                imageLoader.displayImage(url2.toString()
                                          ,thumbnail,options, new                                 
                    ImageLoadingListener() {
                    @Override
                    public void onLoadingStarted() {

                       // progressBar.setVisibility(grid.VISIBLE);
                        //  grid.notify();
                    }

                    @Override
                    public void onLoadingFailed(FailReason failReason) {

                       //progressBar.setVisibility(grid.GONE);
                        //  grid.notify();

                    }

                    @Override
                    public void onLoadingComplete(Bitmap bitmap) {


                      //  progressBar.setVisibility(grid.GONE);
                        // grid.notify();
                    }

                    @Override
                    public void onLoadingCancelled() {

                       //  progressBar.setVisibility(grid.GONE);
                        //grid.notify();

                    }
                });

Solution

  • You haven't defined default caching options in DisplayImageOption.

    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
        .threadPoolSize(5)
        .threadPriority(Thread.MIN_PRIORITY + 3)
        .denyCacheImageMultipleSizesInMemory()
        // 1MB=1048576 
        .memoryCacheSize(1048576 * 5)
        .discCache(new UnlimitedDiscCache(cacheDir))
        .build();
    

    Here, cacheDir is a directory may be on SD card ( requires permission "android.permission.WRITE_EXTERNAL_STORAGE" ) or application's cache directory.

    I have provided cacheDirectory in my application.

    File cacheDir = new File(this.getCacheDir(), "name of directory");
        if (!cacheDir.exists())
            cacheDir.mkdir();
    

    Now, provide your configuration to ImageLoader by following code before downloading any image,

    imageLoader.init(config);