android – 如何使用cordova文件/文件系统根插件访问外部存储?

前端之家收集整理的这篇文章主要介绍了android – 如何使用cordova文件/文件系统根插件访问外部存储?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
问题描述:
我可以使用文件文件系统根(读写)访问内部存储.但是,这样的文件无法从其他应用程序访问.例如,如果我想通过emailComposerPlugin发送此文件,该邮件客户端无法访问该文件. (与“打开”功能相同).
如果我将选项{sandBoxed:true}更改为false(写入外部存储),则它不起作用,最终在FileUtils.UNKNOWN_ERR中.我尝试了应用程序,而手机从USB断开连接,因为一些文档提到外部存储在PC上安装时无法访问 – 同样的结果.

mailing list我读的这个应该是可能的.似乎我错过了一个关键点?

语境:
我尝试启用一个为iPhone创建的混合应用程序在Android设备上运行.要有一个小游乐场,我创建一个小测试项目.

编辑:
文件系统根和文件插件之间似乎存在问题.但我有两个最新版本. (文件:1.0.1文件系统根:0.1.0)
调试文件系统和文件显示

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中

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

AndroidManifest.xml中

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

JavaScript代码

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
    }
}

解决方法

在挖掘整个话题后,我想出了:

>没有必要的文件系统根插件.
> config.xml中需要更多的配置.
>您不需要使用标准的FileApi方式,而是使用以下方式访问文件.

JavaScript用法

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中

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

AndroidManifest.xml中

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
原文链接:https://www.f2er.com/android/311508.html

猜你在找的Android相关文章