PhoneGap的 - 无法写入到文件为Android [英] PhoneGap - Unable to Write to File for Android
问题描述
我正与PhoneGap的版本1.8(科尔多瓦 - 1.8.0.js),并在尝试使针对Android的应用程序。我调试通过的Eclipse在实际设备上。
I am working with PhoneGap version 1.8 (cordova-1.8.0.js) and am attempting to make an app for Android. I am debugging on an actual device through Eclipse.
我试图写文本文件。我使用的PhoneGap所提供的例子API code在
I am trying to write text to a file. I am using the example API code provided by PhoneGap at
http://docs.phonegap.com/en/ 1.8.0 / cordova_file_file.md.html#的FileWriter
我已经能够用我的当前设置了创建的应用程序,所以我知道它运作良好,我只是无法写入文件。我花了几个小时就这个问题,但不断收到以下错误消息:
I have already been able to create apps with my current set-up so I know it works well, I am just unable to write to a file. I have spent hours on this problem but keep getting the following error message:
E/Web Console(27356): Uncaught ReferenceError: LocalFileSystem is not defined at file:///android_asset/www/debug.js:28
第28行之中
line 28 being
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
我使用的API网站的确切code。
I am using the exact code from the API website.
什么我可以做错了什么建议?
Any suggestions on what I could be doing wrong?
也许我不引进我的PhoneGap设置适当的Java库?
Maybe I am not importing the proper Java libraries in my PhoneGap setup?
我有以下的输入:
import android.os.Bundle;
import org.apache.cordova.*;
import android.view.WindowManager;
import android.view.KeyEvent;
感谢您。
编辑:
我用
$(document).ready(function () {
代替的
document.addEventListener("deviceready", onDeviceReady, false);
由于它永远不会开火。这是我的确切code,我也确实有
As it would never fire. Here is my exact code, Also I do have the
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在我的PhoneGap项目活跃。
active within my phonegap project.
满code:
<!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>
我也扔在了设备准备呼叫警报功能,它永远不会被调用的。
I also threw in an alert function on the device ready call, it never gets called at all.
在如何code不工作有什么想法?
Any thoughts on how this code isn't working?
我能永远得到code火在启动时使用的唯一方法
The only way I can ever get code to fire on start up is using
$(document).ready(function () {
});
这是不好的我猜试图调用该文件writter因为我不断收到同样的错误信息。
Which isn't good I guess for trying to call the file writter as I keep getting the same error message.
推荐答案
这个问题不是写一个文件,如果 deviceready
不点火 - 它与如何你的PhoneGap应用程序是安装程序。
The issue isn't with writing a file if deviceready
isn't firing -- it's with how your phonegap app is setup.
您必须导入您的主要活动如下:
You must import the following in your main activity:
import android.app.Activity; // used to fire deviceready
import android.os.Bundle;
import org.apache.cordova.*;
这篇关于PhoneGap的 - 无法写入到文件为Android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!