我正在尝试在 JavaScript 中使用 cordova 的 api 在 PERSISTENT(sd 卡)存储中创建一个文件。我能够创建文件,但无法向其中写入数据(读取时数据为空)。下面是我的 JS 代码:
$(document).ready(function() {
document.addEventListener("deviceready", onDeviceReady, false);
});
function onDeviceReady() {
writeTestList();
}
function writeTestList() {
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFileSystemForWrite, function() { showMsg('Unable to access file system for write'); });
}
function gotFileSystemForWrite(fileSystem) {
fileSystem.root.getFile('testList.txt', {create: true, exclusive: false}, gotFileEntryForWrite, function() { showMsg('Unable to access file for write'); });
}
function gotFileEntryForWrite(fileEntry) {
fileEntry.createWriter(gotFileWriter, function() { showMsg('Unable to create test list'); });
}
function gotFileWriter(writer) {
writer.onwrite = function(evt) {
// show a message
alert('created the test lists');
readSavedContent();
};
writer.onerror = function(evt) {
alert(JSON.stringify(evt));
};
writer.write("raj");
alert(writer.length);
}
控件总是进入 writer 的 onerror 内部,并在提醒事件 obj 时,我看到一个类型:“error”,错误:“JSON error”。我完全不明白这一点。我正在写入一个 txt 文件,但收到 json 错误。我已将写权限放入 android list 中。此外,我在 config.xml 首选项中启用了 cordova 的存储插件。人们,请帮我解决这个问题..!!
最佳答案
无法在 cordova 3.0 中修复此问题。移至 cordova 2.9,一切正常。
关于javascript - Apache cordova - 无法写入android中的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19082010/