How To Use universal image loader offline caching? How To Use universal image loader offline caching? android android

How To Use universal image loader offline caching?


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


But if you restart your device or clean your cache by anyway it will not work offline as the cached files will be deleted.
To Show offline images you have to download images on sd-card to a specific folder and have to pick from there in case on offline.


Don't Know much about such lib. but you can use the following method to download your images.Just Call it from an Async. Task and pass your url.

private File root = Environment.getExternalStorageDirectory();private File dir = new File(root.getAbsolutePath() + "/ImageFolder");private void downloadFile(String url) throws Exception {    URL ur = new URL(url);    String fileName = url.substring(url.lastIndexOf("/") + 1);    File file = new File(dir, fileName);    URLConnection uconn = ur.openConnection();    InputStream is = uconn.getInputStream();    BufferedInputStream bufferinstream = new BufferedInputStream(is);    ByteArrayBuffer baf = new ByteArrayBuffer(5000);    int current = 0;    while ((current = bufferinstream.read()) != -1) {        baf.append((byte) current);    }    FileOutputStream fos = new FileOutputStream(file);    fos.write(baf.toByteArray());    fos.flush();    fos.close();}