I want to clear my application cache programatically in Android Marshmallow 6.0. I tried the following code, but it's not working in Marshmallow.I read on stack overflow that below code is deprecated from API level 19. I add CLEAR_APP_CACHE permission in my Manifests.xml
public void trimCache(Context context) {
try {
File dir = context.getCacheDir();
if (dir != null && dir.isDirectory()) {
deleteDir(dir);
}
} catch (Exception e) {
// TODO: handle exception
}
}
public 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;
}
}
}
// The directory is now empty so delete it
return dir.delete();
}