Skip to content Skip to sidebar Skip to footer

Writing And Reading File In Phonegap

I tried writing/reading a file in phonegap+android, here is the set up: $(document).ready(function() { document.addEventListener('deviceready', deviceready, true); $(docum

Solution 1:

This also works in Android 2.2. You call the load(); function from body's onLoad, and writeFileFromSDCard(string) from some button's onClick, passing as parameter the string you want to write in the file.

<scripttype="text/javascript"charset="utf-8">// Event listener    functionload() {
    document.addEventListener('deviceready', init, false);
 }

 // Called when device is ready - Do nothing functioninit() {
 }      

// Called to write file to cardfunctionwriteFileFromSDCard(param) {

    var writer = newFileWriter("/sdcard/write.txt");
    writer.write(param + "\n", false);              
    alert("file Written to SD Card");
}

</script>

Solution 2:

I would try using the FileReady and FileWriter APIs.

http://docs.phonegap.com/phonegap_file_file.md.html#FileReader

Solution 3:

Here is what I came up with based on several links. I had been searching to do this as well. I used this site as a reference http://www.digitalnoiz.com/mobile-development/mobile-file-explorer-with-phonegapcordova-and-jquery-mobile-part-1/ as well as the Phonegap document api references

functiondisplayMessage(msg)
{
    navigator.notification.alert(msg);
}

functionloadDirectories(fileSystem)
{
    directoryEntry = fileSystem.root;

    var directoryReader = directoryEntry.createReader();

    directoryReader.readEntries(function(entries){
            var sOutput = "";
            for(var i=0; i < entries.length; i++)
            {
                if(!entries[i].isDirectory)
                {
                    fileSystem.root.getFile(entries[i].name,null,gotFileEntry,fail);
                }
            }                            
            //displayMessage(sOutput);
        },fail);
}
functiongotFileEntry(fileEntry)
{
    fileEntry.file(function(file){
        var reader = newFileReader();
        reader.onloadend = function(evt){
        displayMessage(evt.target.result);
    };
reader.readAsText(file);                        
    },fail);
}
functionfailFile(evt)
{
    displayMessage(evt.target.error.code);
}
functionfail(error)
{
    displayMessage("Failed to list directory contents: " + error.code);
}   
functiononBodyLoad()
{       
document.addEventListener("deviceready", onDeviceReady, false);
}       
functiononDeviceReady()
{
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, loadDirectories, fail);
}

Solution 4:

The following works for me on Android with phonegap-1.0.0:

<scripttype="text/javascript"charset="utf-8"src="css-js/phonegap-1.0.0.js"></script><scripttype="text/javascript"charset="utf-8">// Wait for PhoneGap to load//document.addEventListener("deviceready", onDeviceReady, false);

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

    }

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

    }

    functiongotFileEntry(fileEntry) {

        fileEntry.createWriter(gotFileWriter, fail);
    }

    functiongotFileWriter(writer) {
        writer.onwrite = function(evt) {
            console.log("write success");
        };
        writer.write("some sample text");
</script>

Post a Comment for "Writing And Reading File In Phonegap"