【发布时间】:2013-03-24 21:06:12
【问题描述】:
我正在尝试通过online build service 构建一个PhoneGap 应用程序,该应用程序应该在iOS 和Android 上运行,但这个问题集中在Android 部分。
应用程序的主要目标是能够访问和修改文件系统。 受Raymond Camden's blog post 的启发,我最终编写了一个与他非常相似的示例应用程序,它以读/写权限访问文件系统。主要区别在于我的应用程序是在线构建的,没有安装任何 SDK,也没有关心任何 androidManifes.xml 文件。
我的问题是我能够访问文件系统(列出目录、读取文件),但我不能在上面写任何东西。
我已包含necessary <feature /> tag in the confix.xml 以获得文件访问权限:
<feature name="http://api.phonegap.com/1.0/file"/>
这是我的应用程序中使用的一些示例代码:
读取文件代码:
// Request fileSystem
fileSystem.root.getFile(fileName, {create:true}, readFile, onError);
// Function that reads a file
function readFile(file){
var reader = new FileReader();
reader.onloadend = function(e) {
console.log("contents: ", e.target.result);
}
reader.readAsText(file);
}
写入/追加文件代码:
fileSystem.root.getFile(fileName, {create:true}, function(file){
file.createWriter(function(writer) {
writer.onwrite = function() {
console.log('writing', arguments);
}
writer.onerror = function(e) {
console.error('error', e);
}
writer.onwriteend = function() {
console.log('writeend', arguments);
}
//Go to the end of the file...
writer.seek(writer.length);
// Append a timestamp
writerOb.write("Test at "+new Date().toString() + "\n");
})
}, onError);
第二个代码示例没有在目标文件中写入任何内容,onerror 处理程序显示这是因为NOT_FOUND_ERR。这没有任何意义,因为我能够读取同一个文件(并且可以找到)。
以同样的方式,当我尝试创建一个新文件时(使用 写入/追加文件代码中的相同代码,但目标文件不存在),我得到一个 @ 987654337@错误。
我也尝试了the example in the official documentation,得到了相同的结果(读取和不写入)。
请注意,由于 PhoneGap 使用的是 HTML5 文件 API,因此我尝试按照 this Stack Overflow answer(和其他网站)中的建议通过 blobs 写入内容,但没有任何运气。
我错过了什么?我是否需要一个单独的插件来编写文件,或者这是无法通过在线构建工具完成的事情,我必须下载 SDK 并以老式方式编译应用程序?
P.S.:我的 PhoneGap 版本是 2.3.0。
【问题讨论】:
-
手机有SD卡吗?正常编译也行吗?
-
@Warpzit 是的,手机有 SD 车,目标文件在上面;不,我只通过 build.phonegap.com 尝试过
-
fileName 的实际值是多少?你是想写在根目录还是子目录?
标签: javascript android cordova phonegap-plugins phonegap-build