gpt4 book ai didi

javascript - build.phonegap 写入文件的问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:02:57 24 4
gpt4 key购买 nike

我正在尝试通过 PhoneGap 构建一个 online build service 应用程序,它应该在 iOSAndroid 上运行,但这个问题集中在 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。这完全没有意义,因为我能够读取同一个文件(并且可以找到)。

以同样的方式,当我尝试创建一个新文件时(使用来自 write/append file code 的相同代码,但目标文件不存在),我得到一个 INVALID_MODIFICATION_ERR错误。

我也试过 the example in the official documentation,我得到了相同的结果(读和不写)。

请注意,由于 PhoneGap 使用的是 HTML5 文件 API,因此我尝试按照 blobs(以及其他站点)中的建议通过 this Stack Overflow answer 写入内容,但没有成功。

我错过了什么?我是否需要一个单独的插件来编写文件,或者这是否无法通过在线构建工具完成,我必须下载 SDK 并以老式方式编译应用程序?

附言:我的 PhoneGap 版本是 2.3.0

最佳答案

我创建了 an app使用写入文件系统的 PG 构建,所以我确信它可以完成并且不需要插件。我在我的代码中看到的主要显着差异是我明确地将 getFile 选项中的独占标志设置为 false。

如果您正在使用 iOS 进行测试,另一种暗中解决方案是检查您是否在 config.xml 中正确设置了您的应用程序 ID

编辑:在我提到的应用程序中,文件系统编写代码是 here

关于javascript - build.phonegap 写入文件的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15783287/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com