3

I am trying to read a text file with JavaScript using Phonegap 2.0.0 on Android. I went over several examples using the File API at http://docs.phonegap.com/en/2.0.0/cordova_file_file.md.html#File Nothing seems to work.

I read several related threads on Stackoverflow, for example:

How to read file in android using PhoneGap Javascript

Android HTML5 FileApi can i read file from SD Card?

and How to create a new file among others. Yet none of the techniques seem to work.

I am using Nexus One with API 8. The android app loads a URL: http://www.mydomain.com/read_text_file.html which contains the JavaScript utilizing the PhoneGap framework. I am trying to access a file on the SD card located at: /sdcard/textfile.txt. I specify the path to the file in the JavaScript as: file:///sdcard/textfile.txt

For the time being my goal is to trigger an alert from the JavaScript upon page load and show the file contents ( which is just "test file" )

Community
  • 1
  • 1
Bai
  • 93
  • 1
  • 2
  • 5

1 Answers1

3

In Place of "file:///sdcard/textfile.txt" try to use only "textfile.txt".

It seems to work fine.

  • Looks like PhoneGap takes care of the details of the path. I tried this on a couple of devices and the first part of the filename is relative to the sdcard root. For example "textfile.txt" means the file at the root of the sdcard, no matter what the actual path to the sdcard is. Also works with directories, for example "mydir/myfile.txt". If you need the URL you'll get it with fileEntry.toURL() after you have the entry. – Pasi Välkkynen May 24 '13 at 11:03