简体   繁体   English

从SD卡删除文件夹

[英]Delete folders from SD card

I have some folders in my SD card. 我的SD卡中有一些文件夹。 I want to delete folders in closing activity ie in onDestroy() callback. 我想在关闭活动即onDestroy()回调中删除文件夹。 for example : I have folderA, folderB and FolderC. 例如:我有folderA,folderB和FolderC。 I am able to delete folderA and FolderB. 我可以删除FolderA和FolderB。 these folder contains files(.zip, .pdf etc) but folderC I am not able to delete it contains folders, subfolders and files. 这些文件夹包含文件(.zip,.pdf等),但是folderC我无法删除包含文件夹,子文件夹和文件的文件。 Below is my code. 下面是我的代码。

deletUnZipedFiles(File file){
file = new File(MainActivity.root_sd,"/folderC");   
if (file.isDirectory()) {
   String[] children = file.list();
    for (int i=0; i<children.length; i++) {
    boolean success = deletUnZipedFiles(new File(file, children[i]));
    System.out.println("status of unziped delet"+success);
    if (!success) {
      return false;
     }
    }
  }
  // The directory is now empty so delete it
 //return file.delete();
}

Why folderC is not deleting. 为什么folderC没有删除。 I am using same procedure for folderA and folderB. 我对FolderA和FolderB使用相同的过程。

Thanks 谢谢

You can use this function for delete folder. 您可以使用此功能删除文件夹。 You need to pass File object like : 您需要传递File对象,如:

File file = new File("C:\\A\\B"); 
DeleteRecursive(file);

void DeleteRecursive(File fileOrDirectory) {

 if (fileOrDirectory.isDirectory())
    for (File child : fileOrDirectory.listFiles())
        DeleteRecursive(child);

    fileOrDirectory.delete();

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM