In order to remove a directory within Android internal storage, this is the kind of code I use.
val directory = getDir("myDirName", MODE_PRIVATE) // *1.
if (directory.isDirectory()) {
directory.delete()
}
The first problem is that line *1 will create the directory in case it does not exist.
Is there a way to actually know if a directory exists or not, without creating one if it does not?
Beside, I also noticed that this code is not working one hundred percent of the time.
Is there a better way to remove a directory?
CodePudding user response:
You could implement something like that:
val directory = File("myDirName")
if (directory.exists()) {
directory.delete()
}
CodePudding user response:
this is a method which delete your app directory
public boolean deleteRecursive(Context context,String filePath) {
boolean result=false;
//Create androiddeft folder if it does not exist
File fileOrDirectory = new File(filePath);
if (fileOrDirectory.isDirectory()) {
for (File child : fileOrDirectory.listFiles()) {
child.delete();
}
}else {
log("Delete pdf","no directory found");
return false;
}
if (fileOrDirectory.listFiles().length<2)
{
result=true;
}else {
result=false;
}
return result;
}