在phonegap中写入和读取文件

Dem*_*ick 6 filesystems android cordova

我试过writing/reading一个文件phonegap+android,这里是设置:

$(document).ready(function() {
    document.addEventListener("deviceready", deviceready, true);

    $(document).bind("deviceready", function(){
    //writeFile();
    //readFile();
    });
});

function deviceready() {
    writeFile();
    readFile();
}

// This is just to do this.
function readFile() {
    var d = navigator.file.read('/sdcard/foo.xml', success(), fail());
    console.warn(d);
}

function writeFile() {
    navigator.file.write('/sdcard/foo.xml', "This is a test of writing to a file",
            success(), fail());
}
Run Code Online (Sandbox Code Playgroud)

但在Android 2.2的模拟器上,我收到以下错误消息:

08-06 14:21:29.428: INFO/Web Console(936): Error in success callback: Network Status1 = TypeError: Result of expression 'navigator.file' [undefined] is not an object. at file:///android_asset/www/phonegap.0.9.6.js:649
Run Code Online (Sandbox Code Playgroud)

可能会遗漏什么,可以尝试什么?

Dem*_*ick 0

以下内容适用于我在使用phonegap-1.0.0的Android上:

<script type="text/javascript" charset="utf-8" src="css-js/phonegap-1.0.0.js"></script>
    <script type="text/javascript" charset="utf-8">

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

    // PhoneGap is ready
    //
    function onDeviceReady() {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

    }

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

    }

    function gotFileEntry(fileEntry) {

        fileEntry.createWriter(gotFileWriter, fail);
    }

    function gotFileWriter(writer) {
        writer.onwrite = function(evt) {
            console.log("write success");
        };
        writer.write("some sample text");
</script>
Run Code Online (Sandbox Code Playgroud)