开始的时候我一直想努力删除文件夹,后来才发现如果某个路径目录下,如果还存在文件夹而且文件夹里面还有文件。那么你使用file.delete是不起作用。我们必须进入到指定的目录下先把其文件删除再删除。经过一番纠结真的是抓烂头发终于给我憋出来的。废话不说上代码。
public static void deleteFolderFile(String filePath,boolean dedeleteThisPath){
try {
String rootPathSD = Environment.getExternalStorageDirectory().getPath();
File file = new File(rootPathSD + filePath);//获取SD卡指定路径
File[] files = file.listFiles();//获取SD卡指定路径下的文件或者文件夹
for (int i = 0; i < files.length; i++) {
if (files[i].isFile()){//如果是文件直接删除
File photoFile = new File(files[i].getPath());
Log.d("photoPath -->> ", photoFile.getPath());
photoFile.delete();
}else {
if (dedeleteThisPath) {//如果是文件夹再次迭代进里面找到指定文件路径
File[] myfile = files[i].listFiles();
for (int d = 0; d < myfile.length; d++) {
File photoFile = new File(myfile[d].getPath());
photoFile.delete();
}
}
}
}
}catch (Exception e){
e.printStackTrace();
}
}
这样就可以删除指定目录下生成文件夹里面的文件了