我希望能够在 cordova 应用程序中写入/读取文件。
我做过的步骤:
1. ..> Cordova 创建应用程序
2. app>cordova平台添加android
3. app>cordova插件添加https://git-wip-us.apache.org/repos/asf/cordova-plugin-file.git
4. 我已将以下代码添加到项目中并添加到 list 中:
<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
复制它。为我工作。
关于android - 在 cordova 混合 android 应用程序中写入/读取文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22170064/