build.phonegap写入文件的问题

时间:2013-04-03 09:15:58

标签: javascript android cordova phonegap-plugins phonegap-build

我正在尝试通过应在PhoneGaponline build service上运行的iOS构建Android应用,但此问题主要关注Android部分。< / p>

应用程序的主要目标是能够访问和修改文件系统。 受到Raymond Camden's blog post的启发,我最终编写了一个与他非常相似的示例应用程序,它以读/写权限访问文件系统。主要区别在于我的应用程序是在线构建的,没有安装任何SDK,也没有关心任何 androidManifes.xml 文件。

我的问题是我能够访问文件系统(列出目录,读取文件),但我无法在其上写任何内容。

我已添加necessary <feature /> tag in the confix.xml以获得文件访问权限:

<feature name="http://api.phonegap.com/1.0/file"/>

以下是我的应用程序中使用的一些示例代码:

读取文件代码:

// Request fileSystem
fileSystem.root.getFile(fileName, {create:true}, readFile, onError);

// Function that reads a file
function readFile(file){
    var reader = new FileReader();
    reader.onloadend = function(e) {
        console.log("contents: ", e.target.result);
    }
    reader.readAsText(file);
}

编写/附加文件代码:

fileSystem.root.getFile(fileName, {create:true}, function(file){
    file.createWriter(function(writer) {
        writer.onwrite = function() {
            console.log('writing', arguments);
        }

        writer.onerror = function(e) {
            console.error('error', e);
        }

        writer.onwriteend = function() {
            console.log('writeend', arguments);
        }

        //Go to the end of the file...
        writer.seek(writer.length);

        // Append a timestamp
        writerOb.write("Test at "+new Date().toString() + "\n");
    })
}, onError);

第二个代码示例不会在目标文件中写入任何内容,而onerror处理程序会显示它是因为NOT_FOUND_ERR。这只是没有意义,因为我能够读取同一个文件(它可以找到)。

以同样的方式,当我尝试创建一个新文件(使用与写/附加文件代码相同的代码,但目标文件不存在的地方)时,我得到一个{ {1}}错误。

我也尝试了the example in the official documentation并得到了相同的结果(读取和不写入)。

请注意,由于PhoneGap正在使用HTML5文件API,因此我尝试按blobs(以及其他网站)中的建议通过this Stack Overflow answer编写内容,但没有任何运气。

我错过了什么?我是否需要一个单独的插件来编写文件,或者这是通过在线构建工具无法完成的,我必须下载SDK并以老式的方式编译应用程序?

P.S。:我的PhoneGap版本 2.3.0

3 个答案:

答案 0 :(得分:2)

我创建了an app,其PG编译写入文件系统,因此我确信它可以完成并且不需要插件。我在代码中看到的主要显着差异是我明确地将getFile选项中的exclusive标志设置为false。

如果您正在使用iOS测试另一个黑暗解决方案,那就是检查您是否在config.xml中正确设置了应用ID

编辑:在我提到的应用程序中,文件系统编写代码为here

答案 1 :(得分:2)

这是我用于写入文件的Java方法:

public void writeMyFile(String fileName, String data) throws IOException {

    File root = Environment.getExternalStorageDirectory();
    System.out.println(root);
    File gpxfile = new File(root, fileName);

    FileWriter writer = new FileWriter(gpxfile);
    String[][] recordvalue = new String[100][100];
    System.out.println("fetched value" + data);
    String[] line = data.split("#");

    // I had multiple lines of data, all split by the # symbol in a single string.
    System.out.println("row length "+line.length);

        for (int i=1; i<line.length; i++)
        {
            writer.append("#");
            recordvalue[i] = line[i].split(",");
            for(int j=0; j<recordvalue[i].length; j++) {
                System.out.println(recordvalue[i][j]);
                writer.append(recordvalue[i][j]);
                writer.append(",");
            }
            writer.append("\n");
        }

    Uri uri = Uri.fromFile(gpxfile);
    System.out.println(uri);

    writer.flush();
    writer.close();

    try {
        // Do something
    }
    catch (Exception e) {
        // If there is nothing that can send a text/html MIME type
        e.printStackTrace();
    }
}

类/方法的导入如下:

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.cordova.api.PluginResult;
import org.json.JSONArray;

import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.util.Log;

import com.phonegap.api.Plugin;

插件的主要执行类如下:

public PluginResult execute(String arg0, JSONArray arg1, String arg2) {
    try {
         String fileName = arg1.getString(0);
         String data = arg1.getString(1);
         writeMyFile(fileName, data);
    }
    catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return null;
}

答案 2 :(得分:0)

首先,确保以正确的方式拨打requestFileSystem并在设备准备好后

function onDeviceReady() {
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, yourCode, fail);
}

其次,确保 fileName 只是没有路径的文件的名称,如果要使用目录,则应使用DirectoryEntry