5

我正在使用 PhoneGap 1.8 版 (cordova-1.8.0.js) 并尝试为 Android 制作应用程序。我正在通过 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>

我还在设备就绪调用中加入了一个警报功能,它根本不会被调用。

关于此代码如何不起作用的任何想法?

我可以让代码在启动时触发的唯一方法是使用

$(document).ready(function () { 

});

当我不断收到相同的错误消息时,我想这对尝试调用文件写入器并不好。

4

1 回答 1

1

如果没有触发,问题不在于写入文件deviceready- 在于您的 phonegap 应用程序的设置方式。

您必须在主要活动中导入以下内容:

import android.app.Activity; // used to fire deviceready 
import android.os.Bundle;
import org.apache.cordova.*;
于 2012-06-12T02:02:13.043 回答