如何使用cordova文件/文件系统根插件访问外部存储? [英] How to access external storage with cordova file / file-system-roots plugins?

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

问题描述

问题描述:
我可以使用文件或文件系统根(读写)访问内部存储。但这样的文件无法从其他应用程序访问。例如,如果我想通过emailComposerPlugin发送此文件,电子邮件客户端无法访问该文件。
如果我将选项 {sandboxed:true} 更改为false(写入外部存储器),它不工作,最终在一个FileUtils.UNKNOWN_ERR。我尝试应用程序,而手机断开与USB,因为一些文件提到外部存储器无法访问,而挂载在电脑上 - 同样的结果。

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 docs mentioned 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 seems I miss a crucial point?

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

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.

编辑:
似乎有一个问题,系统根和文件插件。但我有他们两个最新的版本。 (文件:1.0.1文件系统根:0.1.0)
调试文件系统和文件类显示

There seems 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

我尝试了什么?

config.xml

config.xml

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

AndroidManifest.xml

AndroidManifest.xml

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

javascript代码

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:


  • 文件系统根插件。

  • config.xml中需要更多配置。

  • 您需要使用不是标准的FileApi方式,

JavaScript用法:

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 cbFail: {function} a callback method that receives a FileError object.

config.xml

config.xml

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

AndroidManifest.xml

AndroidManifest.xml

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

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

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