How To Use universal image loader offline caching?

Hossein Kurd picture Hossein Kurd · Sep 22, 2014 · Viewed 8.4k times · Source

Is it possible to catch offline using universal image loader? If possible, how to use it? Using configs? How To Set Download Directory manually?

out of memory erroron load huge images :

my codes :

DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .cacheOnDisc(true).cacheInMemory(true)
            .imageScaleType(ImageScaleType.IN_SAMPLE_INT)
            .displayer(new FadeInBitmapDisplayer(300)).build();
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(G.appConfigs.context)
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
            .diskCacheExtraOptions(480, 800, null)
            .memoryCache(new WeakMemoryCache())
            .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
            .memoryCacheSize(2 * 1024 * 1024)
            .discCacheSize(300 * 1024 * 1024)
            .build();
    ImageLoader.getInstance().init(config);
    // END - UNIVERSAL IMAGE LOADER SETUP

    ImageLoader imageLoader = ImageLoader.getInstance();

        DisplayImageOptions options = new DisplayImageOptions.Builder().cacheInMemory(true)
            .cacheOnDisc(true).resetViewBeforeLoading(true)
            .showImageForEmptyUri(R.drawable.no_pic)
            .showImageOnFail(R.drawable.load_failed)
            .showImageOnLoading(R.drawable.img_thumb).build();

    //download and display image from url
    imageLoader.displayImage(imgURL, img, options);

how to resolve it ?

Answer

matiash picture matiash · Nov 9, 2014

You can use the ImageLoaderConfiguration.Builder class to customize disk caching. This includes the methods:

  • diskCacheExtraOptions()
  • diskCacheSize() (in bytes).
  • diskCacheFileCount()
  • diskCacheFileNameGenerator()
  • and some others.

Or you can just use diskCache(DiskCache) to provide a custom class for implementing offline caching.

From the example in the Configuration section of the wiki:

File cacheDir = StorageUtils.getCacheDirectory(context);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
    .memoryCacheExtraOptions(480, 800) // default = device screen dimensions
    .diskCacheExtraOptions(480, 800, null)
    .taskExecutor(...)
    .taskExecutorForCachedImages(...)
    .threadPoolSize(3) // default
    .threadPriority(Thread.NORM_PRIORITY - 1) // default
    .tasksProcessingOrder(QueueProcessingType.FIFO) // default
    .denyCacheImageMultipleSizesInMemory()
    .memoryCache(new LruMemoryCache(2 * 1024 * 1024))
    .memoryCacheSize(2 * 1024 * 1024)
    .memoryCacheSizePercentage(13) // default
    .diskCache(new UnlimitedDiscCache(cacheDir)) // default
    .diskCacheSize(50 * 1024 * 1024)
    .diskCacheFileCount(100)
    .diskCacheFileNameGenerator(new HashCodeFileNameGenerator()) // default
    .imageDownloader(new BaseImageDownloader(context)) // default
    .imageDecoder(new BaseImageDecoder()) // default
    .defaultDisplayImageOptions(DisplayImageOptions.createSimple()) // default
    .writeDebugLogs()
    .build();