简体   繁体   English

在Cordova混合Android应用中写入/读取文件

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

I want to be able to write/read to file in cordova app. 我希望能够在Cordova应用程序中写入/读取文件。

the steps I've did: 我所做的步骤:
1. ..>cordova create app 1. ..> cordova创建应用
2. app>cordova platform add android 2. app> cordova平台添加android
3. app>cordova plugin add https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git 3. app> cordova插件添加https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git
4. here I've added following code to the project and added to the manifest: 4. 在这里,我将以下代码添加到项目中并添加到清单中:

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

and added to config.xml: 并添加到config.xml中:

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

5.app>cordova build 5.app> cordova构建
6. app>cordova run android 6. app> cordova运行android
7. output on my android device: 7. 在我的android设备上输出:

a. onready    
b. fail: Class not found.

Why I get this error in the line: window.requestFileSystem(...)? 为什么我在以下行中遇到此错误: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>

You are probably missing the cordova.js file in www/ . 您可能缺少www/cordova.js文件。 Copy it from platforms/android/platform_www/cordova.js . platforms/android/platform_www/cordova.js复制它。 Worked for me. 为我工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM