2011-08-06 30 views
6

tôi đã cố gắng writing/reading một tập tin trong phonegap+android, đây là thiết lập:Viết và tập đọc trong 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()); 
} 

Nhưng trên giả lập cho Android 2.2, tôi nhận được thông báo lỗi sau:

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 

Điều gì có thể bị thiếu và những gì có thể được thử?

+1

có thể là bạn đang sử dụng thành công() thay vì thành công trong cuộc gọi của bạn trở lại cùng đi cho Fail() nên không –

Trả lời

-1

Các công trình sau đây đối với tôi trên Android với 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

bạn vừa đăng mã ví dụ. thậm chí không liên kết để cung cấp tín dụng. – jlarson

+0

Điều này thậm chí không đọc tập tin –

+0

ghi tệp hoạt động tốt cho tôi –

1

Tôi sẽ thử sử dụng API FileReady và FileWriter.

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

+0

Thông báo lỗi nói rằng 'navigator' là không xác định, không có vấn đề gì tôi đặt sau nó, e. g. tệp hoặc tệpMgr. Tôi thậm chí đã thử đặt các phương pháp trong bản demo chính thức, nơi thiết lập phải là 100% OK. – Demonick

2

này cũng hoạt động trong điều hành Android 2.2. Bạn gọi hàm load(); chức năng từ onLoad của cơ thể, và writeFileFromSDCard (chuỗi) từ onClick của một số nút, chuyển thành tham số chuỗi bạn muốn viết trong tệp.

<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

Tôi có thể hỏi bạn rằng tại sao bạn biết cách sử dụng API: FileWriter? – Stallman

+0

hỏi bạn tại sao bạn biết cách sử dụng API: FileWriter? Tôi tham chiếu đến http://www.w3.org/TR/file-system-api/#dfn-filewriter nhưng nó không đề cập đến công cụ này: FileWriter ("đường dẫn"); Và khi tôi sử dụng lớp Media (~), cài đặt cho đường dẫn không cần "/ sdcard /" và tại sao chúng ta cần thêm đường dẫn này vào FileWriter? Tôi đã tìm thấy ít mô tả về API. Xin vui lòng giúp đỡ, cảm ơn !!! – Stallman

+0

Chúng ta có thể vượt qua con đường nào? Tôi cũng không tìm thấy bất kỳ thông tin cụ thể về điều đó? – trainoasis

1

Đây là những gì tôi nghĩ ra dựa trên một số liên kết. Tôi cũng đang tìm cách làm điều này. Tôi đã sử dụng trang web này để tham khảo http://www.digitalnoiz.com/mobile-development/mobile-file-explorer-with-phonegapcordova-and-jquery-mobile-part-1/ cũng như các tài liệu tham khảo api Phonegap tài liệu

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); 
} 
Các vấn đề liên quan