Writing and reading file in phonegap

后端 未结 4 1569
傲寒
傲寒 2020-12-09 22:49

I tried writing/reading a file in phonegap+android, here is the set up:

$(document).ready(function() {
    document.addEventListene         


        
相关标签:
4条回答
  • 2020-12-09 23:19

    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.

    <script type="text/javascript" charset="utf-8">
    
     // Event listener    
     function load() {
        document.addEventListener('deviceready', init, false);
     }
    
     // Called when device is ready - Do nothing 
       function init() {
     }      
    
    // Called to write file to card
       function writeFileFromSDCard(param) {
    
        var writer = new FileWriter("/sdcard/write.txt");
        writer.write(param + "\n", false);              
        alert("file Written to SD Card");
    }
    
    </script>
    
    0 讨论(0)
  • 2020-12-09 23:21

    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

    function displayMessage(msg)
    {
        navigator.notification.alert(msg);
    }
    
    function loadDirectories(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);
    }
    function gotFileEntry(fileEntry)
    {
        fileEntry.file(function(file){
            var reader = new FileReader();
            reader.onloadend = function(evt){
            displayMessage(evt.target.result);
        };
    reader.readAsText(file);                        
        },fail);
    }
    function failFile(evt)
    {
        displayMessage(evt.target.error.code);
    }
    function fail(error)
    {
        displayMessage("Failed to list directory contents: " + error.code);
    }   
    function onBodyLoad()
    {       
    document.addEventListener("deviceready", onDeviceReady, false);
    }       
    function onDeviceReady()
    {
        window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, loadDirectories, fail);
    }
    
    0 讨论(0)
  • 2020-12-09 23:28

    I would try using the FileReady and FileWriter APIs.

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

    0 讨论(0)
  • 2020-12-09 23:39

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

    <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>
    
    0 讨论(0)
提交回复
热议问题