在cordova混合Android应用程序中写入/读取文件

时间:2014-03-04 10:59:35

标签: android cordova io hybrid-mobile-app

我希望能够在cordova app中写/读文件。

我所做的步骤:
 1. ..> cordova创建app
 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&gt; cordova build
 6. app&gt; 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>

1 个答案:

答案 0 :(得分:1)

您可能遗漏了cordova.js中的www/文件。从platforms/android/platform_www/cordova.js复制它。为我工作。