To always clear the user repository (SDCard), you can check the lastModified () date of the file for the givend age and delete it.
For instance:
private void checkTempFiles() {
Log.d(TAG, "--> checkTempFiles");
String tempDirectoryPath = Environment.getExternalStorageDirectory()
.toString() + "/YourTempDirectory";
File dir = new File(tempDirectoryPath);
if (dir.exists() && dir.isDirectory()) {
String[] fileToBeDeleted = dir.list();
for (int i = 0; i < fileToBeDeleted.length; i++) {
File deleteFile = new File(tempDirectoryPath + "/"
+ fileToBeDeleted[i]);
Long lastmodified = deleteFile.lastModified();
if (lastmodified + 86400000L < System.currentTimeMillis()) {
if (deleteFile.isFile()) {
deleteFile.delete();
}
}
}
}
}
source
share