【问题标题】:Ionic-angular, how to get root directory of the SD card?Ionic-angular,如何获取 SD 卡的根目录?
【发布时间】:2017-06-02 19:03:47
【问题描述】:

我正在使用 Ionic 框架来做一个文件浏览器应用程序,但仅限于 SD 卡内。要访问 de SD 卡,我使用带有 File.externalRootDirectory 的 File 插件,并从中检索它的目录列表。这是代码:

constructor(public navCtrl: NavController, private fileCtrl: File) {
    this.goToDir("");
}

public goToDir(dir: string){
    this.fileCtrl.listDir(this.fileCtrl.externalRootDirectory, dir).then(
        (list) => {
            this.loadList(list);
        }
    );
}

public loadList(list: any){
    for (let i=0; i<list.length; i++)
    {
        console.log(list[i].fullPath);
    }
 }

这不会返回我 SD 卡中的目录,而是给我设备内部存储的目录列表(dirs = Android、DCIM、下载等)。

我正在 Android 4.4 和 6.0 中进行测试,我已将 Ionic 和 Cordova 更新到最新版本。

我是 Ionic 的新手,我看不出我做错了什么。如果有人可以提供帮助,那就太好了。

【问题讨论】:

    标签: cordova ionic2 cordova-plugins


    【解决方案1】:

    这不会返回我 SD 卡中的目录,而是给我设备内部存储的目录列表(dirs = Android、DCIM、下载等)。

    据此,我假设您正在尝试访问物理的外部可移动 SD 卡(例如在三星 Galaxy S7 中),而不是 Android 所指的“内部 SD 卡”,它实际上是内部存储器。

    例如,在 Android 7.1 中,我的物理外部可移动 SD 卡路径是 /storage/4975-1401/。

    而我的“内部”SD 卡路径是 /storage/emulated/0/。

    cordova-plugin-file 不允许您访问物理外部可移动 SD 卡:对我来说 cordova.file.externalRootDirectory 返回 file:///storage/emulated/0/。

    但是,您可以使用cordova-diagnostic-plugin 的getExternalSdCardDetails() 来检索物理外部可移动SD 卡的文件路径:

    cordova.plugins.diagnostic.getExternalSdCardDetails(function(details){
        details.forEach(function(detail){
            if(detail.type == "root"){
                cordova.file.externalSdCardRootDirectory = detail.filePath;
            }
        });
    }, function(error){
        console.error(error);
    });
    

    【讨论】:

    • 你是对的。我通过将根目录设置为我的 SdCard 的静态根目录(“file:///storage/extSdCard”)做了一个解决方法,它有效,但您的解决方案似乎好多了。有时间我会试试的。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-08-26
    • 2013-05-25
    • 2012-06-14
    • 1970-01-01
    • 2011-01-09
    相关资源
    最近更新 更多