Question

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

Was it helpful?

Solution

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top