簡體   English   中英

在Cordova混合Android應用中寫入/讀取文件

[英]write/read to file in cordova hybrid android app

我希望能夠在Cordova應用程序中寫入/讀取文件。

我所做的步驟:
1. ..> cordova創建應用
2. app> cordova平台添加android
3. app> cordova插件添加https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git
4. 在這里,我將以下代碼添加到項目中並添加到清單中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  

並添加到config.xml中:

<feature name="File">
    <param name="android-package" value="org.apache.cordova.FileUtils" />
</feature>

5.app> cordova構建
6. app> cordova運行android
7. 在我的android設備上輸出:

a. onready    
b. fail: Class not found.

為什么我在以下行中遇到此錯誤:window.requestFileSystem(...)?

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

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

// device APIs are available
//
function onDeviceReady() {
    alert('onready');
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}

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

function gotFileEntry(fileEntry) {
    fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
    writer.onwriteend = function(evt) {
        console.log("contents of file now 'some sample text'");
        writer.truncate(11);
        writer.onwriteend = function(evt) {
            console.log("contents of file now 'some sample'");
            writer.seek(4);
            writer.write(" different text");
            writer.onwriteend = function(evt){
                console.log("contents of file now 'some different text'");
            }
        };
    };
    writer.write("some sample text");
}

function fail(error) {
    alert('fail: '+error.code);
}

</script>

您可能缺少www/cordova.js文件。 platforms/android/platform_www/cordova.js復制它。 為我工作。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM