如何清除缓存Android

时间:2011-08-01 11:58:13

标签: android list caching clear lazy-evaluation

我需要找到一种方法来清除我的应用程序存储在缓存中的数据。基本上我正在使用Fedor的(Lazy load of images in ListView)惰性列表实现,我想在我有例如100时自动清除缓存图片加载。任何想法如何做到这一点?

修改 代码:

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    list=(ListView)findViewById(R.id.list);
    adapter=new LazyAdapter(this, mStrings);
    list.setAdapter(adapter);

    deleteCache(this);
    adapter.notifyDataSetChanged();


}

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            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();
}

6 个答案:

答案 0 :(得分:36)

这将删除缓存

public static void deleteCache(Context context) {
    try {
        File dir = context.getCacheDir();
        if (dir != null && dir.isDirectory()) {
            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();
}

答案 1 :(得分:4)

我希望这有助于你进一步发展

public static void trimCache(Context context) {
    File dir = context.getCacheDir();
    if(dir!= null && dir.isDirectory()){
        File[] children = dir.listFiles();
        if (children == null) {
            // Either dir does not exist or is not a directory
        } else {
            File temp;
            for (int i = 0; i < children.length; i++) {
                temp = children[i];
                temp.delete();
            }
        }

    }

} 

答案 2 :(得分:1)

您可以致电系统服务 clearApplicationUserData()(仅适用于&gt; = KitKat版本)

所以你可以检查版本,然后一切都会好起来的:) 这是代码:

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            ((ActivityManager) getActivity().getSystemService(Context.ACTIVITY_SERVICE))
                    .clearApplicationUserData();
    }

答案 3 :(得分:0)

如果使用Apache Commons IO,您可以使用一行(不包括try-catch)来完成此任务:

getIntent().getStringExtra("notification_tag");

答案 4 :(得分:0)

使用 Kotlin,您只需调用

File(context.cacheDir.path).deleteRecursively()

结果与其他答案相同,但无需手动检查和循环

答案 5 :(得分:-3)

丢弃旧适配器并将listview附加到适配器的新实例(但listview将丢失滚动位置)。例如:

    @Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    m_adapter = new MyAdapter(this);
        final ListView listView = (ListView) getView()
                .findViewById(R.id.my_list);
        listView.setAdapter(m_adapter);
    }
}