Search code examples
androidimagecachinguniversal-image-loader

Expire UIL cached images after 2 days in android


I am using Universal Image Library (UIL) to load images in my application.

Now problem is that I can have different images on same URL. I mean today I have a cat picture in a URL and tomorrow I might have a dog picture on same URL. So I want to check that if cached image is 2 days old then delete it and download the image again.

This is my configuration.

    File cacheDir = StorageUtils.getOwnCacheDirectory(this, "FriendListCache");
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(this)
        // You can pass your own memory cache implementation
        .diskCache(new UnlimitedDiscCache(cacheDir)) 
        .diskCacheFileNameGenerator(new HashCodeFileNameGenerator())
        .build();
    ImageLoader.getInstance().init(config);

And this is image loading options.

imageLoader = ImageLoader.getInstance();
     options = new DisplayImageOptions.Builder()
    .showImageOnLoading(R.drawable.medium_load)
    .cacheInMemory(true)
    .cacheOnDisk(true)
    .imageScaleType(ImageScaleType.EXACTLY_STRETCHED)
    .decodingOptions(resizeOptions)
    .displayer(new RoundedBitmapDisplayer(160))
    .build();

Solution

  • UIL provide LimitedAgeDiscCache option for your requirement.

    LimitedAgeDiscCache (Size-unlimited cache with limited files' lifetime. If age of cached file exceeds defined limit then it will be deleted from cache.)

    .discCache(new LimitedAgeDiscCache(cacheDir, 14400))