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

Zusee Weekin
  • 1,348
  • 2
  • 18
  • 41
Chris
  • 73
  • 1
  • 8

2 Answers2

0

Please refer PHONEGAP DOCUMENT

From this you will get working example of getting file from sdcard. And if you have problem in downloading a file please refer LINK

I think in Place of "/sdcard/backup.txt" try to use only "backup.txt".

It seems to work fine.

PhoneGap takes care of the details of the path ,also works with directories.

Community
  • 1
  • 1
Suhas Gosavi
  • 2,170
  • 19
  • 40
  • Well, that's what i'm trying since about 2 weeks and you see that my code is practically phonegap docu. If I use only "backup.txt" as path, the program is looking for this file in the application "file" directory. My question is still, how to access a file in any directory on the SDCARD that I choose myself ? Phonegap doesn't take care of it, how can it know which directory I want to use if i cannot write it into the code ? Please give concrete example to help. – Chris Feb 28 '14 at 13:51
0

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:

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

in mosync Reload client.

"Compiling" the app with mosync apk would require only 4 times "../" because the file hierarchy there is lower than in the Reload Client. In any case have to check in file explorer (root access required). Different from that is when you compile in the cloud with phonegap - then indeed the root path is sdcard ! 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. In this case I could write it, but the reader stops without error message.

Hope that will help someone else as well !

Chris

Chris
  • 73
  • 1
  • 8
  • Hello Chris, could you post the code ? i try to copy/paste your first code in my apache cordova project and it's doesn't work. I search for two weeks how file cordova function works :/ two posts here. – zelocalhost May 01 '14 at 03:45