PhoneGap - 将数据保存到本地文件系统(File API)

时间:2013-02-18 22:03:17

标签: android file cordova save

我刚刚开始使用PhoneGap的File API并且在保存过程中遇到了一些问题。这是javascript代码:

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);
}

更新

<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>

但是在设备文件系统中没有这样的文件(course.oerk)。这是一块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.

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:1)

固定。只需将saveCourseToFile();移动到锚点:

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