8

I'm using Universal Image Loader to display images in my app in listviews. I'm using UnlimitedDiscCache since this is the fastest cache mechanism according to the documentation.

However, I would like to clear the disc cache when my app is closed (e.g. in onStop()) but only the oldest cached files that exceed a given limit should be deleted (like TotalSizeLimitedDiscCache does).

I am aware of ImageLoader.clearDiscCache() but in my case this clears the complete cache since I am using UnlimitedDiscCache before...

So I would like to have the fastest cache mechanism when the user is loading and scrolling the listviews and do the slow cache clear when the user is no longer interacting with the app.

Any ideas how I can achieve this?

jeff_bordon
  • 392
  • 4
  • 16
  • You can't guarantee that the app is stopping when `onStop()` is called. That only says that the current activity is being stopped. The performance loss is very minimal when compared to time spent implementing another caching mechanism (which may slow down the app anyways). – Austyn Mahoney Jul 31 '13 at 21:24
  • @jeff_bordon Do you have any code that is used to build the ImageLoader instance? @Androidy Yes you can differentiate when the app is about to finish, like `if(isFinishing()) { do something}` – Nikola Despotoski Aug 07 '13 at 00:37
  • @jeff_bordon you can have BoB(best of both). See the source here https://github.com/nostra13/Android-Universal-Image-Loader/blob/master/library/src/com/nostra13/universalimageloader/cache/disc/impl/LimitedAgeDiscCache.java Writing UnlimitedAgeDiskCache will be pretty straight forward. All you have to know is the date and difference between now and then. I think it will not decrease the speed of the ListViews if it is matter of few ifs. – Nikola Despotoski Aug 07 '13 at 00:48
  • `isFinishing()` only tells you that the current activity is stopping, not the app. If you are only using one `Activity` sure, but otherwise you will have issues. – Austyn Mahoney Aug 07 '13 at 16:50

2 Answers2

1

check this from here https://stackoverflow.com/a/7763725/1023248 may help you..

@Override
protected void onDestroy() {
// closing Entire Application
android.os.Process.killProcess(android.os.Process.myPid());
Editor editor = getSharedPreferences("clear_cache", Context.MODE_PRIVATE).edit();
editor.clear();
editor.commit();
trimCache(this);
super.onDestroy();
}


public static void trimCache(Context context) {
try {
    File dir = context.getCacheDir();
    if (dir != null && dir.isDirectory()) {
        deleteDir(dir);

    }
} catch (Exception e) {
    // TODO: handle exception
}
}


public static boolean deleteDir(File dir) {
if (dir != null && dir.isDirectory()) {
    String[] children = dir.list();
    for (int i = 0; i < children.length; i++) {
        boolean success = deleteDir(new File(dir, children[i]));
        if (!success) {
            return false;
        }
    }
}

// <uses-permission
// android:name="android.permission.CLEAR_APP_CACHE"></uses-permission>
// The directory is now empty so delete it

return dir.delete();
}
Community
  • 1
  • 1
Deepak Swami
  • 3,838
  • 1
  • 31
  • 46
0

If you want to clear the cache from the memory, you can use the following code:

MemoryCacheUtils.removeFromCache(imageUri, ImageLoader.getInstance().getMemoryCache());

If you also want to clear the cache in the disk, you can use the following code:

DiscCacheUtils.removeFromCache(imageUri, ImageLoader.getInstance().getDiscCache());

This is a much simpler way of clearing the cache. You can find similar answers in this thread:

How to force a cache clearing using Universal Image Loader Android?

I hope this information was helpful. :)

Community
  • 1
  • 1
Cyril Noah
  • 15
  • 1
  • 8