I have done done lots of googling and searching here on StackOverflow for an answer to my question. I have seen the many posts about a recursive file deletion method (like this, this, and this), but none of these have worked for me. I am using this method to find my SD card (which is /storage/0000-0000
).
private void wipeSDCards() {
File storage = new File("/storage");
File[] storageSubDirs = storage.listFiles();
for (File storageSubDir : storageSubDirs) {
try {
boolean storageSubDirIsEmulated = Environment.isExternalStorageEmulated(storageSubDir);
boolean storageSubDirIsRemovable = Environment.isExternalStorageRemovable(storageSubDir);
if (!storageSubDirIsEmulated && storageSubDirIsRemovable) {
deleteStuff(storageSubDir);
}
} catch (IllegalArgumentException iae) {
LoggingUtil.i(MyClass.class, String.format(Locale.US, "FAILED ON: %s", storageSubDir.getAbsolutePath()));
}
}
}
private void deleteStuff(File fileOrDirectory) {
// things I have tried ....
}
To delete the files on the SD card I have tried:
File fileToDelete = new File('path.to.file');
fileToDelete.delete();
But that doesn't work. I have tried:
Files.delete(fileToDelete.toPath());
But that doesn't work. I have tried:
application.getContentResolver().delete(uri, null, null);
But that doesn't work either. I have placed this print statement just before all my deletion attempts:
Log.e("LOOK HERE: ", String.format(Locale.US, "Your Application %s delete this file (%s)", (fileOrDirectory.canWrite() ? "can" : "can NOT"), fileOrDirectory.getName()));
And have always gotten back Your Application can NOT delete this file (path.to.file)
. It seems that I do not have the authorization to delete files on the SD card, even though I can through the Windows file browser.
So, is an application allowed to delete the contents of the SD card? If so, how does one go about doing this? How about a method to just format the SD card, I would take that as well.
Also, please don't ask "why do you want to do this" ... I just do.