我正在使用Android版本1.8 (cordova-1.8.0.js),并试图为PhoneGap制作一个应用程序。我正在通过Eclipse在一个实际的设备上调试。
我正在尝试将文本写入文件。我正在使用PhoneGap提供的示例API代码。
http://docs.phonegap.com/en/1.8.0/cordova_file_file.md.html#FileWriter
我已经能够用我当前的设置创建应用程序,所以我知道它工作得很好,只是我无法写入文件。我花了几个小时来解决这个问题,但一直收到以下错误消息:
E/Web Console(27356): Uncaught ReferenceError: LocalFileSystem is not defined at file:///android_asset/www/debug.js:28第28行是
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);我使用的是API网站上的代码。
对我可能做错的地方有什么建议吗?
也许我没有在我的PhoneGap设置中导入正确的Java库?
我导入了以下内容:
import android.os.Bundle;
import org.apache.cordova.*;
import android.view.WindowManager;
import android.view.KeyEvent;谢谢。
编辑:
我用的是
$(document).ready(function () {而不是
document.addEventListener("deviceready", onDeviceReady, false);因为它永远不会开火。这是我的确切代码,我也有
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />在我的phonegap项目中很活跃。
完整代码:
<!DOCTYPE html>
<html>
<head>
<title>FileWriter Example</title>
<script type="text/javascript" charset="utf-8" src="cordova-1.8.0.js"></script>
<script type="text/javascript" charset="utf-8">
// Wait for Cordova to load
//
document.addEventListener("deviceready", onDeviceReady, false);
// Cordova is ready
//
function onDeviceReady() {
alert("device ready"); // this never gets called
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
function gotFS(fileSystem) {
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) {
console.log(error.code);
}
</script>
</head>
<body>
<h1>Example</h1>
<p>Write File</p>
</body>
</html>我还在设备就绪调用中抛出了一个alert函数,它根本不会被调用。
对这段代码为什么不能工作有什么想法吗?
我可以让代码在启动时触发的唯一方法是使用
$(document).ready(function () {
});我想这对于尝试调用文件写入器是不好的,因为我总是得到相同的错误消息。
发布于 2012-06-12 10:02:13
如果deviceready没有启动,问题不是写文件--而是你的phonegap应用程序是如何设置的。
您必须在主活动中导入以下内容:
import android.app.Activity; // used to fire deviceready
import android.os.Bundle;
import org.apache.cordova.*;https://stackoverflow.com/questions/10989656
复制相似问题