Android Delete Directory Not Working
Solution 1:
Here is your problem;
Your recursion is almost right... Here's the problem.
If files[i] is a directory, (you rightfully go to clear everything in it) However, once "everything" in files[i] is deleted, you do not actually delete files[i] itself.
So the recursion is incomplete; What you need to do is remove the "else" within the forloop.
That way when files[i] which is a directory, will be cleaned up after all things in it have been removed. You were just leaving it.
To be more specific:
publicvoiddeleteFile(String uri)
{
FilecurrentFile=newFile(uri);
File files[] = currentFile.listFiles();
for (inti=0; i < files.length; i++)
{
if (files[i].isDirectorty())
{
deleteFiles(files[i].toString());
}
//no else, or you'll never get rid of this folder!
files[i].delete();
}
}
Solution 2:
Have you set the proper permissions - WRITE_EXTERNAL_STORAGE in your manifest. Easy to overlook sometimes :/
Look at the source topic, I think that guy got it to work...
Source: Delete a folder on SD card
Solution 3:
How are you verifying that the folder is left over? Sometimes the DDMS file explorer is out of sync and you need to refresh it before you see the directory go away.
Solution 4:
try {
FiletempFolder=newFile(path);
for (File f : tempFolder.listFiles()){
if (!f.isDirectory()) {
f.delete();
}
tempFolder.delete();
}
} catch (Exception e) {
e.printStackTrace();
}
Post a Comment for "Android Delete Directory Not Working"