Search code examples
androidcachinguniversal-image-loader

Set life time for image cache by using Android Universa Image Loader Library


In my android project, I am using Android-Universal-Image-Loader Lib (HERE) to load image from internet.

I want to save cache of those images for a period of time (example 1 day).

After the life time ends, it will load image again from internet (when my application starts). Otherwise, it will use cache.

Please help me


Solution

  • Use LimitedAgeDiskCache for that.

    File cacheDir = StorageUtils.getCacheDirectory(context);
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(context)
            ...
            .diskCache(new LimitedAgeDiskCache(cacheDir, 24 * 60 * 60))
            .build();
    
    ImageLoader.getInstance().init(config);