3

I know this question was asked many times before but I cannot figure it out anyhow. I'm able to write a text file to a directory on the sdcard (android 4), but no way to read it back again, using this code:

function get_file () {  
window.requestFileSystem(LocalFileSystem.PERSISTENT, 0, getFilesystem, fail);
}   
function getFilesystem(fs) {
alert("getFilesystem -> backup.txt");  // OK !
// alert("filesystem.name: "+fileSystem.name);  // = persistent
// alert("filesystem.root.name: "+fileSystem.root.name);  // = long number
    // 
fs.root.getFile("../../../../../../sdcard/test/backup.txt", {create: false, exclusive: false}, 
    function(fileEntry) {
      alert(fileEntry.fullPath); // shows that my path is appended to "data/.."
      fileEntry.file(function(file) {
      var reader = new FileReader();
      reader.onloadend = function(evt) {
        alert(+evt.target.result);  // NOT SHOWING !        
          };
        reader.readAsText(file);
    }, fail);
}, fail);
}

I wrote the file using the writer function into the directory sdcard/test using this sequence of ../ - this is an ugly code but working !

But fs.root.getFile does not work in the same way - the fullPath information it returns shows that my path given is APPENDED to "/data/data/com.appname/files" but does not replace it !

The onloadend function obviously isn't working since i never got the alert message, neither did I get an error message. Change of path to "file:///sdcard/test" or "sdcard/test" has no effect either.

Any help is highly appreciated - thank you in advance ! Chris

4

2 回答 2

0

请参考PHONEGAP 文档

从这里您将获得从 sdcard 获取文件的工作示例。如果您在下载文件时遇到问题,请参考LINK

我认为代替“/sdcard/backup.txt”尝试仅使用“backup.txt”。

它似乎工作正常。

PhoneGap 负责路径的细节,也适用于目录。

于 2014-02-28T07:18:16.083 回答
0

所以最后我可以弄清楚:要访问该目录(在 Android 4.0,三星 Note 8 上),我必须逐字逐句地从 /data/data/com.app-name/files/apps/(随机编号)中查找所有目录/ 通过选择返回 SD 卡:

 fs.root.getFile("../../../../../../sdcard/test/backup.txt", ...

在 mosync 重新加载客户端。

使用 mosync apk “编译”应用程序只需要 4 次“../”,因为那里的文件层次结构低于重新加载客户端中的层次结构。在任何情况下都必须签入文件资源管理器(需要 root 访问权限)。与此不同的是,当您使用 phonegap 在云中编译时,根路径确实是 sdcard !然后,据我所知,您无法转到数据目录。

此路径适用于 mosync 中的写入和读取 - 我的错误是我在文本文件中有换行符“\n”。在这种情况下,我可以写它,但读者会停止而没有错误消息。

希望这也会对其他人有所帮助!

克里斯

于 2014-03-04T09:06:15.520 回答