2

PhoneGap - How to create a new file in assets/www/example/hallo.text my error is "Error fileSystem"

window.requestFileSystem = window.requestFileSystem || window.webkitRequestFileSystem;
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

function fail() {
  confirm('Error  fileSystem');
}

function gotFS(fileSystem) {
  fileSystem.root.getFile("file:///android_asset/www/example/hallo.text", {
    create: true
  }, gotFileEntry, fail);
}

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

function gotFileWriter(writer) {
  writer.write("hallo");
}
Manse
  • 37,765
  • 10
  • 83
  • 108
samibel
  • 634
  • 2
  • 6
  • 20

2 Answers2

2

The HTML file system API operates in a sandboxed file system. You cannot reference absolute paths with respect to the full filesystem; instead, all of your Web app's file system exists in a browser-generated subdirectory inside the device's real firesystem.

This was done deliberately, for security reasons, so that Web apps can only overwrite or read files that they have created. Otherwise, any page on the Web might read all the private contents of your whole hard drive.

You should use an unqualified file path like www/example/hallo.text (assuming those directories exist) or simply hallo.text to create the file in your sandboxed file system. Do not use an absolute file: path.

Additionally, your error function, fail, is probably being supplied with an argument with error info. Try adding an argument to the function like function fail(error) ... and printing out error for more debugging info.

apsillers
  • 112,806
  • 17
  • 235
  • 239
  • Is that when you use a path starting with `file://` or without? – apsillers Jun 26 '12 at 12:59
  • with file:// getFile("file:///android_asset – samibel Jun 26 '12 at 13:15
  • Then you need to remove it, as I said in my answer. Unfortunately, you cannot accomplish what you are trying to do (access a specific file on the real filesystem) with HTML5 or Phonegap. All file operations in HTML5 are in a sandbox file system. – apsillers Jun 26 '12 at 15:04
0

First of all, make sure "you" have rights to write in the destination folder (your device may need to have the root permissions to write in some folders).

After that, follow the instructions from here: http://www.anddev.org/working_with_files-t115.html

Carlos Barcellos
  • 544
  • 1
  • 4
  • 10
  • The OP wants to know how to create a file *in Phonegap*, an HTML5-to-native-app bridge. The OP's application is written in JavaScript, not native Android code. – apsillers Jun 26 '12 at 12:42