如何访问外部存储与科尔多瓦文件/文件系统的根插件? [英] How to access external storage with cordova file / file-system-roots plugins?

查看:376
本文介绍了如何访问外部存储与科尔多瓦文件/文件系统的根插件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题描述: 我可以访问内部存储有任何文件或文件系统的根(读写)。但是,这样的文件无法从其他应用程序访问。例如,如果我想通过emailComposerPlugin发送此文件,该文件无法通过电子邮件客户端访问。 (同样适用于功能打开方式。) 如果我更改选项 {沙盒:真} 为假(写入外部存储),这是行不通的,并在FileUtils.UNKNOWN_ERR结束。我尝试了应用程序,而手机从USB断开,因为一些实况提到,尽管安装在PC外部存储设备不能被访问 - 同样的结果,虽然

从我读的邮件列表这应该是可能的。它接缝我错过了关键点?

背景: 我尝试启用为iPhone创建一个混合应用程序在Android设备上运行。有一个小操场上,我创建了一个小的测试项目。

编辑: 有接缝是文件系统的根和文件的插件之间的问题。但我有他们两人的最新版本。 (文件:1.0.1文件系统的根:0.1.0) 调试文件系统和文件类表明

 私人字符串fullPathForLocalURL(URI URL){
    如果(FILESYSTEM_PROTOCOL.equals(URL.getScheme())及&安培;本地主机.equals(URL.getHost())){
        字符串路径= URL.getPath();
        如果(URL.getQuery()!= NULL){
            路径=路径+? + URL.getQuery();
        }
        返回path.substring(path.indexOf('/',1));
        //路径=/缓存外在这一点上
        //导致索引越界异常
 

我有什么企图?

confix.xml

 < preference名=AndroidExtraFilesystems值=文件,文件,外部文件,SD卡,高速缓存,缓存外部/>
 

AndroidManifest.xml中

 <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
 

JavaScript的code

 函数createTextDocument(文件名,文字){

    cordova.filesystem.getDirectoryForPurpose(缓存,{沙盒:假},successCallback,failureCallback);

    功能successCallback(的DirectoryEntry){
        的console.log(目录下找到(科尔多瓦):'+ directoryEntry.toURL());
        的console.log(目录下找到(原生):+ directoryEntry.toNativeURL());
        directoryEntry.getFile(文件名,{创建:真实,独家:假},
            功能(fileEntry){
                变种文件路径= fileEntry.toNativeURL();
                fileEntry.createWriter(
                    功能(的FileWriter){
                        的console.log('开始写:'+文件路径);
                        fileWriter.write(文本);
                        的console.log(文件写入);
                    },failureCallback
                );
            },failureCallback
        );
    }

    功能failureCallback(错误){
        的console.log('错误创建文件:+错误code);
        //结果code 1000
    }
}
 

解决方案

挖掘这整个主题相当多之后,我想通了:

  • 无需为文件系统根插件。
  • 在有需要在config.xml中更多的配置。
  • 您需要用不标准的FileApi方式,但下面的访问文件。

JavaScript的用法:

  window.resolveLocalFileSystemURL(路径,cbSuccess,cbFail);

@param路径:{string}里用方案的科尔多瓦路径:
             cdvfile://本地主机/<文件系统名称> /<路径到文件>
             例如:cdvfile://本地主机/ SD卡/路径/要/全球/文件'
                       cdvfile://localhost/cache/onlyVisibleToTheApp.txt
@param cbSuccess:{}功能一个接收一个DirectoryEntry对象回调方法。
@param cbSuccess:{}功能一个接收一个FileError对象回调方法。
 

confix.xml

 < preference名=AndroidPersistentFileLocation值=兼容性/>
< preference名=AndroidExtraFilesystems值=SD卡,高速缓存/>
 

AndroidManifest.xml中

 <使用-权限的Andr​​oid:名称=android.permission.WRITE_EXTERNAL_STORAG​​E/>
 

Problem description: I can access the internal storage with either the file or the file-system-roots (read and write). But such a file can not be access from an other app. For example if i want to send this file through the emailComposerPlugin, the file can not be accessed by the email client. (Same goes for the "open with" function.) If i change the options {sandboxed: true} to false (to write to the external storage), it does not work and ends up in a FileUtils.UNKNOWN_ERR. I tried the application while the phone disconnected from USB, as some docu mention that external storage can not be accessed while mounted on the pc - same result though.

From what i read on the mailing list this should be possible. It seams i miss a crucial point?

Context: I try to enable an hybrid application created for iPhone to run on android devices. To have a little playground, i create a small test project.

Edit: There seams to be a problem between file-system-roots and file plugin. But i have the newest versions of both of them. (File: 1.0.1 File-system-roots: 0.1.0) Debugging the file-system and file classes show that

private String fullPathForLocalURL(Uri URL) {
    if (FILESYSTEM_PROTOCOL.equals(URL.getScheme()) && "localhost".equals(URL.getHost())) {
        String path = URL.getPath();
        if (URL.getQuery() != null) {
            path = path + "?" + URL.getQuery();
        }
        return path.substring(path.indexOf('/', 1));
        // path = "/cache-external" at this point
        // results in index out of bounds exception

What have i tried?

confix.xml

<preference name="AndroidExtraFilesystems" value="files,files-external,documents,sdcard,cache,cache-external" />

AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

javascript code

function createTextDocument(filename, text) {

    cordova.filesystem.getDirectoryForPurpose('cache', {sandboxed: false}, successCallback, failureCallback);

    function successCallback(directoryEntry){
        console.log('directory found (cordova): ' + directoryEntry.toURL());
        console.log('directory found (native) : ' + directoryEntry.toNativeURL());
        directoryEntry.getFile(filename, {create: true, exclusive: false},
            function(fileEntry){
                var filePath = fileEntry.toNativeURL();
                fileEntry.createWriter(
                    function(fileWriter){
                        console.log('start writing to: ' + filePath );
                        fileWriter.write(text);
                        console.log('file written');
                    },failureCallback
                );
            }, failureCallback
        );
    }

    function failureCallback(error){
        console.log('error creating file: ' + error.code);
        // results in code 1000
    }
}

解决方案

After digging this whole topic quite a bit, i figured out:

  • There is no need for the file system roots plugin.
  • There is more configuration needed in config.xml.
  • You need to use not the standard FileApi way, but the following to access the files.

JavaScript usage:

window.resolveLocalFileSystemURL(path, cbSuccess, cbFail);

@param path: {string} a cordova path with scheme: 
             'cdvfile://localhost/<file-system-name>/<path-to-file>' 
             Examples: 'cdvfile://localhost/sdcard/path/to/global/file'
                       'cdvfile://localhost/cache/onlyVisibleToTheApp.txt'
@param cbSuccess: {function} a callback method that receives a DirectoryEntry object.
@param cbSuccess: {Function} a callback method that receives a FileError object.

confix.xml

<preference name="AndroidPersistentFileLocation" value="Compatibility" />
<preference name="AndroidExtraFilesystems" value="sdcard,cache" />

AndroidManifest.xml

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

这篇关于如何访问外部存储与科尔多瓦文件/文件系统的根插件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆