【问题标题】:Glide: How to clear all App cache (images) when App is logout?Glide:App注销时如何清除所有App缓存(图像)?
【发布时间】:2016-10-08 00:32:10
【问题描述】:

我在我的应用程序中使用 Glide 库。我想在注销过程中完全清除 Glide 完成的所有图像缓存(磁盘和内存)。

注销过程是通过 Job 完成的(我使用了这个库 android-queue)。

我想调用这些行:

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

执行这些行时,我遇到了一些线程问题(不是主线程;如果我使用 Handler,则会出现另一个错误,您必须在后台线程上调用此方法等)

感谢您的帮助!

【问题讨论】:

  • 请从您的 logcat 中发布错误消息

标签: android caching android-glide


【解决方案1】:

如果您只想清除应用程序缓存,可以使用以下代码:

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;
    }
}

你可以把这段代码放在你调用这段代码的地方:

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

【讨论】:

  • 此代码只会清除您应用的整个缓存。我想这就是你要找的东西?您必须将应用的上下文传递给 deleteCache()
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 2018-05-01
  • 2017-02-16
  • 1970-01-01
  • 2021-11-21
  • 1970-01-01
相关资源
最近更新 更多