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

问题描述 投票:0回答:1

当我尝试使用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”,使用import:import {File}来自'@ ionic-native / file';

cordova ionic3 cordova-plugins ionic-native cordova-plugin-file
1个回答
0
投票

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

如果路径是这样的:

文件:///存储/仿真器/ 0 / somefolder

然后获取目录名称如下:

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

如果在最后一个路径中有斜杠(/),例如:

文件:///存储/仿真器/ 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)

希望它能帮到你!!!

© www.soinside.com 2019 - 2024. All rights reserved.