正在删除Android内部存储中的目录



为了删除Android内部存储中的一个目录,我使用了这种代码。

val directory = getDir("myDirName", MODE_PRIVATE) // *1.
if (directory.isDirectory()) {
directory.delete()
}

第一个问题是,如果目录不存在,第*1行将创建该目录。

有没有一种方法可以真正知道目录是否存在,如果不存在,则不创建目录?

此外,我还注意到这个代码在百分之百的情况下都不能工作。

有更好的方法删除目录吗?

您可以实现这样的东西:

val directory = File("myDirName")
if (directory.exists()) {
directory.delete()
}

这是一种删除应用程序目录的方法

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;
}

最新更新