gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 07:58:13 25 4
gpt4 key购买 nike

问题描述:我可以使用文件或文件系统根(读取和写入)访问内部存储。但是这样的文件不能从其他应用程序访问。例如,如果我想通过 emailComposerPlugin 发送此文件,则电子邮件客户端无法访问该文件。 (“打开方式”功能也是如此。)如果我将选项 {sandboxed: true} 更改为 false(写入外部存储),它将不起作用并以 FileUtils.UNKNOWN_ERR 结束。我在手机与 USB 断开连接时尝试了该应用程序,因为一些文档提到在安装在 PC 上时无法访问外部存储 - 但结果相同。

根据我在 mailing list 上阅读的内容这应该是可能的。看来我错过了一个关键点?

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

编辑:file-system-roots 和文件插件之间似乎有问题。但我有他们两个的最新版本。 (文件: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

我尝试了什么?

配置文件

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

配置文件

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

AndroidManifest.xml

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

关于android - 如何使用 cordova 文件/文件系统根插件访问外部存储?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22868089/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com