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

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

问题描述

我使用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控制台(27356):未捕获ReferenceError: LocalFileSystem未在file:///android_asset/www/debug.js中定义:28 

第28行

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

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



对我可能做错了什么建议?



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



我已导入以下内容:

  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项目中有效。



完整代码:

 code><!DOCTYPE html> 
< html>
< head>
< title> FileWriter示例< / title>

; script type =text / javascriptcharset =utf-8src =cordova-1.8.0.js>< / script>
< script type =text / javascriptcharset = utf-8>

//等待Cordova加载
//
document.addEventListener(deviceready,onDeviceReady,false);

// Cordova准备就绪
//
function onDeviceReady(){

alert(device ready); // this never get 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);
}

函数gotFileWriter(writer){
writer.onwriteend = function(evt){
console.log(file of file now'some sample text' );
writer.truncate(11);
writer.onwriteend = function(evt){
console.log(file of'now'some sample');
writer.seek(4);
writer.write(different text);
writer.onwriteend = function(evt){
console.log(file of file now'some text text');
}
};
};
writer.write(some sample text);
}

function fail(error){
console.log(error.code);
}

< / script>
< / head>
< body>
< h1>示例< / h1>
< p>写入文件< / p>
< / body>
< / html>

我也在设备就绪调用上抛出一个警告函数,它从来不会被调用。



有关此代码无效的任何想法吗?



我可以在启动时获取代码的唯一方法是使用

  $(document).ready(function(){

});

这是不好我想试图调用文件写程序,信息。

解决方案

如果 deviceready isn



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

  import android.app.Activity; //用于触发deviceready 
import android.os.Bundle;
import org.apache.cordova。*;


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.

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

line 28 being

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

I am using the exact code from the API website.

Any suggestions on what I could be doing wrong?

Maybe I am not importing the proper Java libraries in my PhoneGap setup?

I have the following imported:

import android.os.Bundle;
import org.apache.cordova.*;
import android.view.WindowManager;
import android.view.KeyEvent;

Thank you.

EDIT:

I was using

$(document).ready(function () {

Instead of the

 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.

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

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.

解决方案

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天全站免登陆