Phonegap - unable to write to Android file

I am working with PhoneGap version 1.8 (cordova-1.8.0.js) and am trying to make an Android app. I am debugging the actual device through Eclipse.

I am trying to write text to a file. I am using the sample API code provided by PhoneGap in

http://docs.phonegap.com/en/1.8.0/cordova_file_file.md.html#FileWriter

I was already able to create applications with my current setup, so I know that this works well, I just can not write the file. I spent hours on this problem, but keep getting the following error message:

E/Web Console(27356): Uncaught ReferenceError: LocalFileSystem is not defined at file:///android_asset/www/debug.js:28

line 28 is

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

I am using the exact code from the API website.

Any suggestions on what I might be doing wrong?

Perhaps I am not importing the proper Java libraries into the PhoneGap setup?

I have imported:

import android.os.Bundle;
import org.apache.cordova.*;
import android.view.WindowManager;
import android.view.KeyEvent;

Thank.

EDIT:

I used

$(document).ready(function () {

Instead

 document.addEventListener("deviceready", onDeviceReady, false);

. .

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

phonegap.

:

<!DOCTYPE html>
<html>
<head>
<title>FileWriter Example</title>

<script type="text/javascript" charset="utf-8" src="cordova-1.8.0.js"></script>
<script type="text/javascript" charset="utf-8">

// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);

// Cordova is ready
//
function onDeviceReady() {

   alert("device ready"); // this never gets called

    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

function gotFS(fileSystem) {
    fileSystem.root.getFile("readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

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

function gotFileWriter(writer) {
    writer.onwriteend = function(evt) {
        console.log("contents of file now 'some sample text'");
        writer.truncate(11);  
        writer.onwriteend = function(evt) {
            console.log("contents of file now 'some sample'");
            writer.seek(4);
            writer.write(" different text");
            writer.onwriteend = function(evt){
                console.log("contents of file now 'some different text'");
            }
        };
    };
    writer.write("some sample text");
}

function fail(error) {
    console.log(error.code);
}

</script>
</head>
<body>
<h1>Example</h1>
<p>Write File</p>
</body>
</html>

, .

, ?

, , -

$(document).ready(function () { 

});

, , -, .

+5
1

, deviceready , , .

:

import android.app.Activity; // used to fire deviceready 
import android.os.Bundle;
import org.apache.cordova.*;
+1

All Articles