当前位置: 动力学知识库 > 问答 > 编程问答 >

android - Glide: How to clear all App cache (images) when App is logout?

问题描述:

I user Glide library in my App. And I want to clear completely all images cache (disk and memory) done by Glide, during the logout process.

The logout process is done through a Job (I used this library android-queue).

I want to call these lines:

Glide.get(MyApplication.getInstance()).clearMemory();

Glide.get(MyApplication.getInstance()).clearDiskCache();

I have some troubles about thread when these lines are executed (not main thread; if I use Handler then this another error appears YOu must call this method on a background thread, etc.)

Thanks for your help!

网友答案:

If you just want to clear application cache you can use following code:

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        deleteDir(dir);
    } catch (Exception e) {}
}

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;
            }
        }
        return dir.delete();
    } else if(dir!= null && dir.isFile()) {
        return dir.delete();
    } else {
        return false;
    }
}

You can place this code exactly same place where you are calling this code:

Glide.get(MyApplication.getInstance()).clearMemory();
Glide.get(MyApplication.getInstance()).clearDiskCache();
分享给朋友:
您可能感兴趣的文章:
随机阅读: