简体   繁体   English

使用纯Javascript在Android上使用mosync / phonegap从SD卡读取文件

[英]Read files from SD Card with mosync/phonegap on Android with pure Javascript

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: 我能够将文本文件写入sdcard(android 4)上的目录,但无法再使用此代码再次读取它:

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 ! 我使用编写器函数将文件写入目录sdcard / test,使用这个序列../ - 这是一个丑陋的代码,但正在工作!

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 ! fs.root.getFile不能以相同的方式工作 - 它返回的fullPath信息显示我给出的路径是APPENDED到“/data/data/com.appname/files”但不替换它!

The onloadend function obviously isn't working since i never got the alert message, neither did I get an error message. onloadend函数显然不起作用,因为我从未收到警报消息,也没有收到错误消息。 Change of path to "file:///sdcard/test" or "sdcard/test" has no effect either. 将路径更改为“file:/// sdcard / test”或“sdcard / test”也没有任何效果。

Any help is highly appreciated - thank you in advance ! 任何帮助都非常感谢 - 提前谢谢! Chris 克里斯

Please refer PHONEGAP DOCUMENT 请参阅PHONEGAP文件

From this you will get working example of getting file from sdcard. 从这里你将得到从sdcard获取文件的工作示例。 And if you have problem in downloading a file please refer LINK 如果您在下载文件时遇到问题,请参阅LINK

I think in Place of "/sdcard/backup.txt" try to use only "backup.txt". 我想在“/sdcard/backup.txt”的地方尝试只使用“backup.txt”。

It seems to work fine. 它似乎工作正常。

PhoneGap takes care of the details of the path ,also works with directories. PhoneGap负责路径的细节,也适用于目录。

So finally I could figure it out: To access the directory (on Android 4.0, Samsung Note 8) I had to go up literally all directories from /data/data/com.app-name/files/apps/(random no.)/ back to the sdcard by choosing: 所以最后我可以搞清楚:要访问该目录(在Android 4.0上,三星Note 8)我必须从/data/data/com.app-name/files/apps/(random no。)字面上看所有目录。 /选择退回SD卡:

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

in mosync Reload client. 在mosync重新加载客户端。

"Compiling" the app with mosync apk would require only 4 times "../" because the file hierarchy there is lower than in the Reload Client. 使用mosync apk“编译”应用程序只需要4次“../”,因为文件层次结构低于重新加载客户端。 In any case have to check in file explorer (root access required). 无论如何都要检查文件资源管理器(需要root访问权限)。 Different from that is when you compile in the cloud with phonegap - then indeed the root path is sdcard ! 与此不同的是,当您使用phonegap在云中编译时 - 那么根路径确实是SD卡! You then cannot go to the data directory as far as I could find out. 然后,就我所知,你无法进入数据目录。

This path works with write and read in mosync - my mistake was that I had line break character "\\n" inside the text file. 此路径适用于mosync中的写入和读取 - 我的错误是我在文本文件中有换行符“\\ n”。 In this case I could write it, but the reader stops without error message. 在这种情况下,我可以写它,但读者停止没有错误消息。

Hope that will help someone else as well ! 希望这也会帮助别人!

Chris 克里斯

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

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