this.file.listDir 不会 return applicationStorageDirectory 下的文件夹

this.file.listDir won't return folders under the applicationStorageDirectory

当我尝试列出包含 ionic-native 文件的文件夹目录时,结果没有得到任何文件夹。

我的代码:

this.platform.ready().then(() => {
      this.filesystem.listDir(this.filesystem.applicationStorageDirectory, '').then((result) => {
       console.log(result);
        /*result will have an array of file objects with 
        file details or if its a directory*/
        for (let file of result) {
          if (file.isDirectory == true && file.name != '.' && file.name != '..') {
            console.log("This is a folder");
            // Code if its a folder
          } else if (file.isFile == true) {
            // Code if its a file
            console.log("This is a file");
            let name = file.name // File name
            console.log("file name: " + name);
           // let path = file.path // File path
            file.getMetadata(function (metadata) {
              let size = metadata.size; // Get file size
            })
          }
        }
     });
    });

我正在使用:"@ionic-native/file": "4.12.0", 随着进口: 从“@ionic-native/file”导入{文件};

您没有在 this.filesystem.listDir(this.filesystem.applicationStorageDirectory, '') 中传递目录名称,您必须在此方法中传递目录名称。您可以从 this.filesystem.applicationStorageDirectory 中获取目录名称。首先提醒 this.filesystem.applicationStorageDirectory 并检查您获得的路径类型。

如果路径如下:

file:///storage/emulator/0/somefolder

然后得到如下目录名:

let appStorageDir = "file:///storage/emulator/0/somefolder";
let dirName = appStorageDir.substring(appStorageDir.lastIndexOf("/")+1);

如果路径最后有斜线(/),如:

file:///storage/emulator/0/somefolder/

然后得到如下目录名:

let appStorageDir = "file:///storage/emulator/0/somefolder/";
appStorageDir = appStorageDir.substring(0,appStorageDir.lastIndexOf("/"));
let dirName = appStorageDir.substring(appStorageDir.lastIndexOf("/")+1);

现在将 dirName 传递到方法中,如下所示:

this.filesystem.listDir(this.filesystem.applicationStorageDirectory, dirName)

希望对您有所帮助!!!