gpt4 book ai didi

javascript - cordova 3.x (phonegap) - 写入/数据/数据生成 encodingException

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:15:09 29 4
gpt4 key购买 nike

我正在尝试使用以下摘自 here 的代码在我的应用程序内存中写入一个文件:

    writeOnFileSystem : function() {
console.log("writeOnFileSystem resolveLocalFileSystemURL ...");
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
};

function gotFS(fileSystem) {
fileSystem.root.getFile("file:///data/data/com.company.app/readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
...
}

function fail(error) {
console.log(error.code);
}

这会抛出这样的异常:

05-14 12:16:55.704: W/System.err(27827): org.apache.cordova.file.EncodingException: This path has an invalid ":"in it.

我正在使用这个字符串访问我的/data/data:file:///data/data/com.company.app/readme.txt(com.company.app是我的app的包)

  • 这是访问我的/data/data 的正确方式吗?

如果我在我的 SD 上写入,同样的代码可以工作,这在 Android 上是默认完成的。

我正在使用:

Cordova 3.5.0-0.2.1

org.apache.cordova.file 1.0.1 "file"

org.apache.cordova.file-transfer 0.4.4-dev “文件传输”

质量管理

eclipse

最佳答案

编辑:虽然这个答案仍然有效,但对 Cordova File API 进行了相当多的更改。

无论如何,

当您调用 requestFileSystem 时,它会返回一个FileSystem 对象,它有一个根属性,它是一个 DirectoryEntry

当您调用 resolveLocalFileSystemURI 时,它会返回一个 DirectoryEntry文件入口

所以在你的情况下你需要做:

window.resolveLocalFileSystemURI("file:///data/data/{package_name}", onSuccess, onError); 

function onSuccess(entry) {
entry.getDirectory("example", {create: true, exclusive: false},onGetDirectorySuccess, onGetDirectoryFail);
}
function onError(error){
console.log(error);
}

方法 resolveLocalFileSystemURI 将允许您访问/data/data 文件夹,然后您从那里开始。

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); 的问题是,在 Android 上,如果设备上安装了 SD 卡,它将为您提供 SD 卡路径,否则它会给你内部存储的路径(甚至不确定是 data/data/{package_name} 还是其他地方)。如果你问我,这是有史以来最愚蠢的设计选择之一

关于javascript - cordova 3.x (phonegap) - 写入/数据/数据生成 encodingException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23652193/

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