缓存没有以编程方式清除Android应用程序?

时间:2014-07-30 05:14:08

标签: android caching bitmap

在我的应用程序中因为我使用Bitmap工厂从http url获取位图图像对象很多数据被缓存。所以我编写了以下代码来清除每次应用程序加载时的缓存数据。它类似于我们通过单击设置 - >中的清除缓存按钮手动执行的操作。应用 - > YOUR_APP。

但是,我写的代码不起作用。缓存大小原样。

public void clearApplicationData() {
        File cache = getCacheDir();
        File appDir = new File(cache.getParent());
        if (appDir.exists()) {
            String[] children = appDir.list();
            for (String s : children) {
                if (!s.equals("lib") && !s.equals("shared_prefs")) {
                    deleteDir(new File(appDir, s));
                }
            }
        }
    }

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

我还在我的应用程序的AndroidManifest.xml中添加了以下权限:

<uses-permission android:name="android.permission.CLEAR_APP_CACHE" />

请帮忙。以及我们如何以有效的方式使用Bitmap工厂来降低性能并提高效率。

0 个答案:

没有答案
相关问题