在cordova hybrid android应用程序中写入/读取文件

在cordova hybrid android应用程序中写入/读取文件,android,cordova,io,hybrid-mobile-app,Android,Cordova,Io,Hybrid Mobile App,我希望能够在cordova应用程序中写入/读取文件 我所做的步骤: 1. ..>cordova创建应用程序 2.应用程序>cordova平台添加android 3.应用程序>cordova插件添加 4.在这里,我将以下代码添加到项目并添加到清单中: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 为什么在以下行中出现此错误:window.requestFileSystem(

我希望能够在cordova应用程序中写入/读取文件

我所做的步骤:
1. ..>cordova创建应用程序
2.应用程序>cordova平台添加android
3.应用程序>cordova插件添加
4.在这里,我将以下代码添加到项目并添加到清单中:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />  
为什么在以下行中出现此错误:window.requestFileSystem(…)


//等待加载设备API库
//
文件。添加的监听器(“deviceready”,OnDeviceraddy,false);
//设备API可用
//
函数ondevicerady(){
警报(“onready”);
requestFileSystem(LocalFileSystem.PERSISTENT,0,gotFS,fail);
}
函数gotFS(文件系统){
警报(“gotFS”);
getFile(“readme.txt”,{create:true,exclusive:false},gotFileEntry,fail);
}
函数gotFileEntry(fileEntry){
createWriter(gotFileWriter,失败);
}
函数gotFileWriter(writer){
writer.onwriteend=函数(evt){
log(“文件内容现在是‘一些示例文本’”);
writer.truncate(11);
writer.onwriteend=函数(evt){
log(“文件内容现在是‘一些示例’”);
作者:seek(4);
写(“不同的文本”);
writer.onwriteend=函数(evt){
log(“文件内容现在是‘一些不同的文本’”);
}
};
};
writer.write(“一些示例文本”);
}
功能失败(错误){
警报(“失败:”+错误代码);
}

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

你添加了文件插件吗?@diveh Salian,是的。忘了写那个了。我将编辑问题,以便您的设备就绪警报被称为rgt??我不记得是什么问题(那是2个月前),但我认为你是对的。
<feature name="File">
    <param name="android-package" value="org.apache.cordova.FileUtils" />
</feature>
a. onready    
b. fail: Class not found.
<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>