cordova 3.x(phonegap) - 写入/ data/data生成encodingException

eea*_*dev 3 javascript jquery-mobile cordova cordova-3

我想写使用取自下面的代码我的应用程序存储器中的文件在这里:

    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);
 }
Run Code Online (Sandbox Code Playgroud)

这引发了这样的异常:

05-14 12:16:55.704:W/System.err(27827):org.apache.cordova.file.EncodingException:此路径中包含无效的":".

我正在使用此字符串来访问我的/ data/data: file:///data/data/com.company.app/readme.txt(com.company.app是我的应用程序的包)

  • 这是访问我的/数据/数据的正确方法吗?

如果我在SD上写入相同的代码,默认情况下在Android上完成.

我在用:

科尔多瓦3.5.0-0.2.1

org.apache.cordova.file 1.0.1"文件"

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

JQM

日食

Mir*_*rko 9

编辑:虽然这个答案仍然存在,但Cordova File API有很多变化

无论如何,

当你调用requestFileSystem它时,返回一个 FileSystemroot属性为a的对象DirectoryEntry.

当你打电话给resolveLocalFileSystemURI它时,返回一个DirectoryEntryFileEntry.

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

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);
}
Run Code Online (Sandbox Code Playgroud)

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

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

  • 谢谢,我同意Phonegap/Cordova的支持很差.您也可以尝试官方论坛https://groups.google.com/forum/#!forum/phonegap (2认同)