cordova 3.x(phonegap) - 写on / data / data生成encodingException [英] cordova 3.x (phonegap) - write on /data/data generates encodingException

查看:193
本文介绍了cordova 3.x(phonegap) - 写on / data / data生成encodingException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用以下代码在应用程序内存中写入文件: here

  writeOnFileSystem:function(){
console.log (writeOnFileSystem resolveLocalFileSystemURL ...);
window.requestFileSystem(LocalFileSystem.PERSISTENT,0,gotFS,fail);
}
};

function gotFS(fileSystem){
fileSystem.root.getFile(file:///data/data/com.company.app/readme.txt,{create:true, exclusive:false},gotFileEntry,fail);
}

function gotFileEntry(fileEntry){
fileEntry.createWriter(gotFileWriter,fail);
}

函数gotFileWriter(writer){
...
}

函数失败(错误){
控制台.log(error.code);
}

这会抛出此异常:



05-14 12:16:55.704:W / System.err(27827):org.apache.cordova.file.EncodingException:此路径中包含无效的:。



我正在使用此字符串访问我的/ data / data:
file:///data/data/com.company.app/ readme.txt (com.company.app是我的应用程序的包)




  • 这是访问我的/ data / data?



同样的代码在我在SD上写入时也是如此。

我使用的是:



Cordova 3.5.0-0.2.1



org.apache.cordova.file 1.0.1文件



org.apache.cordova.file-transfer 0.4.4-dev文件传输



JQM



eclipse

解决方案

编辑:此答案仍然有效,但 Cordova File API



无论如何,



当调用 requestFileSystem 时,它返回一个
FileSystem root属性,它是一个 DirectoryEntry



当您调用 resolveLocalFileSystemURI 时,它会返回 DirectoryEntry
FileEntry



因此,在您的情况下,您需要执行:

  resolveLocalFileSystemURI(file:/// data / data / {package_name},onSuccess,onError); 

function onSuccess(entry){
entry.getDirectory(example,{create:true,exclusive:false},onGetDirectorySuccess,onGetDirectoryFail);
}
function onError(error){
console.log(error);
}

方法 resolveLocalFileSystemURI



window.requestFileSystem(LocalFileSystem.PERSISTENT)的问题,你可以访问/ data / data文件夹, 0,gotFS,fail); 是在Android上,它会给你的SD卡路径,如果有一个SD卡安装在设备上,否则它会给你的内部存储的路径甚至确定如果data / data / {package_name}或其他地方)。如果你问我,这是所有时候最愚蠢的设计选择之一


I am trying to write a file on my application memory using the following code taken from here:

    writeOnFileSystem : function() {
    console.log("writeOnFileSystem resolveLocalFileSystemURL ...");     
    window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail);
}
};

function gotFS(fileSystem) {
fileSystem.root.getFile("file:///data/data/com.company.app/readme.txt", {create: true, exclusive: false}, gotFileEntry, fail);
}

function gotFileEntry(fileEntry) {
fileEntry.createWriter(gotFileWriter, fail);
}

function gotFileWriter(writer) {
 ...
}

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

This throws such exception:

05-14 12:16:55.704: W/System.err(27827): org.apache.cordova.file.EncodingException: This path has an invalid ":" in it.

I am using this string to access my /data/data: file:///data/data/com.company.app/readme.txt (com.company.app is the package of my app)

  • Is this the proper way to access my /data/data?

The same code works if I write on my SD which is done by default on Android.

I am using:

Cordova 3.5.0-0.2.1

org.apache.cordova.file 1.0.1 "File"

org.apache.cordova.file-transfer 0.4.4-dev "File Transfer"

JQM

eclipse

解决方案

Edit: while this answer still holds, there are quite a few changes to the Cordova File API

Anyway,

When you call requestFileSystem it returns a FileSystem object which has a root property which is a DirectoryEntry.

When you call resolveLocalFileSystemURI it returns a DirectoryEntry or FileEntry.

So in your case you need to do:

window.resolveLocalFileSystemURI("file:///data/data/{package_name}", onSuccess, onError); 

function onSuccess(entry) { 
    entry.getDirectory("example", {create: true, exclusive: false},onGetDirectorySuccess, onGetDirectoryFail); 
}
function onError(error){
console.log(error);
}

the method resolveLocalFileSystemURI will give you access to the /data/data folder, then you go from there.

The problem with window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, gotFS, fail); is that on Android it will give you the SD card path if there is an SD card mounted on the device, otherwise it will give you the path to the internal storage (not even sure if data/data/{package_name} or somewhere else). If you ask me, this is one of the most stupid design choices of all times

这篇关于cordova 3.x(phonegap) - 写on / data / data生成encodingException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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