I am working with PhoneGap version 1.8 (cordova-1.8.0.js) and am attempting to make an app for Android. I am debugging on an actual device through Eclipse.
I am trying to write text to a file. I am using the example API code provided by PhoneGap at
http://docs.phonegap.com/en/1.8.0/cordova_file_file.md.html#FileWriter
I have already been able to create apps with my current set-up so I know it works well, I am just unable to write to a file. I have 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 being
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
I am using the exact code from the API website.
Any suggestions on what I could be doing wrong?
Maybe I am not importing the proper Java libraries in my PhoneGap setup?
I have the following imported:
import android.os.Bundle;
import org.apache.cordova.*;
import android.view.WindowManager;
import android.view.KeyEvent;
Thank you.
EDIT:
I was using
$(document).ready(function () {
Instead of the
document.addEventListener("deviceready", onDeviceReady, false);
As it would never fire. Here is my exact code, Also I do have the
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
active within my phonegap project.
full code:
<!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>
I also threw in an alert function on the device ready call, it never gets called at all.
Any thoughts on how this code isn't working?
The only way I can ever get code to fire on start up is using
$(document).ready(function () {
});
Which isn't good I guess for trying to call the file writter as I keep getting the same error message.