简体   繁体   English

build.phonegap写入文件的问题

[英]Issues with build.phonegap write to file

I'm trying to build a PhoneGap application through the online build service that should run on both iOS and Android , but this question focuses on the Android part. 我正在尝试通过应在iOSAndroid上运行的在线构建服务构建PhoneGap应用程序,但这个问题主要集中在Android部分。

The main target of the application is to be able to access and modify the filesystem. 该应用程序的主要目标是能够访问和修改文件系统。 Inspired from Raymond Camden's blog post , I ended up writing a sample application very similar to his, that accesses the filesystem with read/write privileges. 受到Raymond Camden博客文章的启发,我最终编写了一个与他非常相似的示例应用程序,它以读/写权限访问文件系统。 The main difference is that my application is built online without any SDK installed and without caring about any androidManifes.xml file. 主要区别在于我的应用程序是在线构建的,没有安装任何SDK,也没有关心任何androidManifes.xml文件。

My problem is that I am able to access the filesystem (list directories, read files), but I am not able to write anything on it. 我的问题是我能够访问文件系统(列表目录,读取文件),但我无法在其上写任何内容。

I have included the necessary <feature /> tag in the confix.xml in order to have file access permissions: confix.xml中包含了必要的<feature />标记,以获得文件访问权限:

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

Here is some sample code used in my application: 以下是我的应用程序中使用的一些示例代码:

Read file code: 读取文件代码:

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

Write/append file code: 写/附加文件代码:

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

The second code sample doesn't write anything in the targeted file and the onerror handlers shows that it's because of a NOT_FOUND_ERR . 第二个代码示例不在目标文件中写入任何内容,并且onerror处理程序显示它是由于NOT_FOUND_ERR And this just doesn't make sense, because I am able to read that same file (and it can be found ). 这只是没有意义,因为我能够读取相同的文件(并且可以找到它)。

In the same manner, when I try to create a new file (with the same code from write/append file code , but where the targeted file doesn't exist), I get a INVALID_MODIFICATION_ERR error. 以同样的方式,当我尝试创建一个新文件(使用相同的代码来写/附加文件代码 ,但目标文件不存在)时,我收到一个INVALID_MODIFICATION_ERR错误。

I have also tried the example in the official documentation and I got the same result (read and no write). 我也在官方文档中尝试了这个例子,我得到了相同的结果(读取和不写入)。

Note that since PhoneGap is using the HTML5 file API , I've tried to write the contents through blobs as suggested in this Stack Overflow answer (and in other sites) without any luck. 请注意,由于PhoneGap正在使用HTML5文件API ,我试图通过blobs编写内容, 如此Stack Overflow应答 (以及其他网站中)所示,没有任何运气。

What am I missing? 我错过了什么? Do I need a separate plugin for writing files or is this something that can't be done via the online build tool and I have to download the SDK and compile the application the old fashioned way? 我是否需要一个单独的插件来编写文件,或者这是通过在线构建工具无法完成的事情,我必须下载SDK并以旧式方式编译应用程序?

PS: My PhoneGap version is 2.3.0 . PS:我的PhoneGap版本是2.3.0

I've created an app with PG build that writes to the file system, so I'm certain it can be done and doesn't require a plugin. 我已经创建了一个带有PG构建的应用程序 ,可以写入文件系统,因此我确信它可以完成并且不需要插件。 The main conspicuous difference I'm seeing in my code is that I'm explicitly setting the exclusive flag in the getFile options to false. 我在代码中看到的主要显着差异是我明确地将getFile选项中的exclusive标志设置为false。

If you're testing with iOS another shot in the dark solution would be to check that you're setting your app id correctly in the config.xml 如果您正在使用iOS测试另一个黑暗解决方案,那就是检查您是否在config.xml中正确设置了应用程序ID

EDIT: In the app I mentioned, there file system writing code is here 编辑:在我提到的应用程序中,文件系统编写代码在这里

Here's the Java method I'm using for writing to a file: 这是我用来写入文件的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();
    }
}

The imports for the class/method are as follows: 类/方法的导入如下:

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;

And the main execute class for the plugin is as follows: 并且插件的主要执行类如下:

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

First, make sure you call requestFileSystem the proper way and after the device is ready 首先,确保以正确的方式调用requestFileSystem并在设备准备好之后

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

Second, make sure fileName is only the name of a file without path, if you want to use directories, you should use DirectoryEntry . 其次,确保fileName只是没有路径的文件的名称,如果要使用目录,则应使用DirectoryEntry

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM