PhoneGap - saving data to the local file system (File API)

I just started playing with the File API in PhoneGap and already had some problems with the save process. Here is the javascript code:

function saveCourseToFile() {
    console.log("checkpoint 1");
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, onFSSuccess, onFSError);
}
function onFSSuccess(fileSystem) {
    console.log("checkpoint 2");
    console.log("Opened file system: " + fileSystem.name);
    fileSystem.root.getFile("course.oerk", {create:true, exclusive:false}, gotFileEntry, onFSError);
}
function gotFileEntry(fileEntry) {
    console.log("checkpoint 3");
    fileEntry.createWriter(gotFileWriter, onFSError);
}
function gotFileWriter(writer) {
    writer.onwrite = function(evt) {
    console.log("checkpoint 4: write success!");
    };
    writer.write("test test test");
}
function onFSError(err) {
    console.log(err.code);
}

      

UPDATE:

<script type="text/javascript">
    function init() {
        document.addEventListener("deviceready", onDeviceReady, false);
    }
</script>
<body onload="init()">
    <div id="page1">
        <a href="#page2">Click me</a>
    </div>
    <div id="page2">
        <script type="text/javascript">
            saveCourseToFile();
        </script>
        <p>Text text text text</p>
    </div>
</body>

      

But there is no such file (course.oerk) in the device's file system. Here is a snippet of the logcat:

02-18 22:36:55.542: D/Cordova(746): onPageFinished(file:///android_asset/www/index.html#coursesPage)
02-18 22:36:55.542: D/Cordova(746): Trying to fire onNativeReady
02-18 22:36:55.552: D/DroidGap(746): onMessage(onNativeReady,null)
02-18 22:36:55.552: D/DroidGap(746): onMessage(onPageFinished,file:///android_asset/www/index.html#coursesPage)
02-18 22:36:56.223: I/Choreographer(746): Skipped 36 frames!  The application may be doing too much work on its main thread.

      

Any help would be appreciated.

+3


source to share


1 answer


Fixed. Just by moving saveCourseToFile();

to the anchor:



....
<div id="page1">
    <a href="#page2" onclick="saveCourseToFile()">Click me</a>
</div>
<div id="page2">
    <p>Text text text text</p>
</div>
....

      

+1


source







All Articles