2011-08-06 388 views
6

我試圖writing/reading一個文件中phonegap+android,這裏是設置:寫入和讀取文件中的PhoneGap

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

但在模擬器的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 

什麼可能會丟失,什麼可以嘗試?

+1

可能定),您使用的成功(),而不是你的呼叫成功背部同樣爲不合格(要失敗 –

回答

-1

對我來說,以下工作在Android上的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> 
+1

。甚至沒有鏈接來給予信貸。 – jlarson

+0

這甚至沒有讀取文件 –

+0

寫文件對我來說工作得很好 –

2

這也適用於Android 2.2的。你調用load();函數從body的onLoad,並從某個按鈕的onClick寫入FileFromSDCard(字符串),作爲參數傳遞你想寫入文件的字符串。

<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

我可以問你,爲什麼你知道如何使用API​​:FileWriter? – Stallman

+0

問你,爲什麼你知道如何使用API​​:FileWriter? 我參考了http://www.w3.org/TR/file-system-api/#dfn-filewriter 但它沒有提到這個工具:FileWriter(「path」);而當我使用Media(〜)類時,路徑的設置不需要「/ sdcard /」,爲什麼我們需要在FileWriter中添加此路徑? 我發現關於API的一些描述。請幫忙,謝謝! – Stallman

+0

我們可以通過哪條路?我也沒有找到任何具體的信息呢? – trainoasis

1

這是我基於幾個鏈接想出來的。我一直在尋找這樣做。我用這個網站作爲一個參考http://www.digitalnoiz.com/mobile-development/mobile-file-explorer-with-phonegapcordova-and-jquery-mobile-part-1/還有Phonegap文件API參考

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