退出应用程序时如何删除我的应用程序缓存?

How do I delete my app cache when the app is exited?

有没有办法在退出时删除应用程序的缓存,因为使用 Glide 从不同的片段和活动中加载 recylerview 中的图像正在为应用程序生成大量缓存。(不想使用磁盘glide 的缓存策略,因为运行时需要缓存)

如果您正在寻找删除您自己的应用程序的缓存,那么只需删除您的缓存目录即可!

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


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