PhoneGap - 无法写入Android文件 [英] PhoneGap - Unable to Write to File for Android

查看:136
本文介绍了PhoneGap - 无法写入Android文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用PhoneGap 1.8版(cordova-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代码

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行是

window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);

我正在使用API​​网站中的确切代码。

I am using the exact code from the API website.

有什么建议可以做错什么?

Any suggestions on what I could be doing wrong?

也许我没有正确导入正确的Java库我的PhoneGap设置?

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);

因为它永远不会发生,这是我的确切代码,另外我有

As it would never fire. Here is my exact code, Also I do have the

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在我的电话沟通项目中有效。

active within my phonegap project.

全部代码:

<!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.

关于这段代码不工作的任何想法?

Any thoughts on how this code isn't working?

启动时,我可以得到代码启动的唯一方法是使用

The only way I can ever get code to fire on start up is using

$(document).ready(function () { 

});

哪个不好我猜是试图调用文件写入器,因为我不断得到相同的错误信息。

Which isn't good I guess for trying to call the file writter as I keep getting the same error message.

推荐答案

如果 deviceready isn不要开机 - 这是你的手机应用程序如何设置。

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆