【发布时间】:2014-04-05 21:02:14
【问题描述】:
我希望能够在 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. 应用>cordova 运行安卓
7. 在我的安卓设备上输出:
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>
【问题讨论】:
-
你添加文件插件了吗?
-
@Divesh Salian,是的。忘了写那个。我将编辑问题
-
所以你的设备就绪警报被称为 rgt ??并分享你的 logcat 详细信息
标签: android cordova io hybrid-mobile-app