Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Universal image loader - clear cache manually

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?

like image 386
jeff_bordon Avatar asked Jun 05 '13 12:06

jeff_bordon


1 Answers

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();
}
like image 161
Deepak Swami Avatar answered Sep 19 '22 15:09

Deepak Swami