Universal image loader - clear cache manually
Asked Answered
O

2

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?

Ogham answered 5/6, 2013 at 12:8 Comment(4)
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).Daffi
@Ogham 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}Sailor
@Ogham you can have BoB(best of both). See the source here github.com/nostra13/Android-Universal-Image-Loader/blob/master/… 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.Sailor
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.Daffi
K
1

check this from here https://mcmap.net/q/150970/-how-to-programmatically-clear-application-data 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();
}
Kennel answered 7/8, 2013 at 18:5 Comment(0)
L
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. :)

Labana answered 22/8, 2016 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.