cordova 3.x (phonegap) - write on /data/data generates encodingException

自古美人都是妖i 提交于 2019-12-10 18:00:35

问题


I am trying to write a file on my application memory using the following code taken from 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);
 }

This throws such exception:

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

I am using this string to access my /data/data: file:///data/data/com.company.app/readme.txt (com.company.app is the package of my app)

  • Is this the proper way to access my /data/data?

The same code works if I write on my SD which is done by default on Android.

I am using:

Cordova 3.5.0-0.2.1

org.apache.cordova.file 1.0.1 "File"

org.apache.cordova.file-transfer 0.4.4-dev "File Transfer"

JQM

eclipse


回答1:


Edit: while this answer still holds, there are quite a few changes to the Cordova File API

Anyway,

When you call requestFileSystem it returns a FileSystem object which has a root property which is a DirectoryEntry.

When you call resolveLocalFileSystemURI it returns a DirectoryEntry or FileEntry.

So in your case you need to do:

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

the method resolveLocalFileSystemURI will give you access to the /data/data folder, then you go from there.

The problem with window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); is that on Android it will give you the SD card path if there is an SD card mounted on the device, otherwise it will give you the path to the internal storage (not even sure if data/data/{package_name} or somewhere else). If you ask me, this is one of the most stupid design choices of all times



来源:https://stackoverflow.com/questions/23652193/cordova-3-x-phonegap-write-on-data-data-generates-encodingexception

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!