Tôi cần tìm cách xóa dữ liệu mà ứng dụng lưu trữ trong bộ nhớ cache. Về cơ bản, tôi đang sử dụng Fedor's (Lazy load of images in ListView) thực hiện danh sách lười và tôi muốn xóa bộ nhớ cache tự động khi có ví dụ 100 hình ảnh được tải. Bất kỳ ý tưởng nào về cách thực hiện điều đó?Cách xóa bộ nhớ cache Android
EDIT: Code:
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();
}
Xin chào, bạn có câu trả lời không? Tôi có cùng một vấn đề như bạn. tôi muốn bộ nhớ cache rõ ràng để tải hình ảnh mới. cảm ơn –