最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - cordova 3.x (phonegap) - write on datadata generates encodingException - Stack Overflow

programmeradmin0浏览0评论

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/pany.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/pany.app/readme.txt (pany.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

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/.pany.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/.pany.app/readme.txt (.pany.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

Share Improve this question asked May 14, 2014 at 10:34 eeadeveeadev 3,85211 gold badges52 silver badges102 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 9

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

发布评论

评论列表(0)

  1. 暂无评论