Reading text file in resources folder: file not found on Android emulator, but works in iOS simulator

You must Login before you can answer or comment on any questions.

Hi, i'm trying to read a text file (a csv like file) located in a subfolder of the Resources folder in my appcelerator folder (it is located on Resources/quizzes/quizzes.qpak and as said is a simple text file with some separators, like csv).

I use the following code to get the file and read it:

var file = Titanium.Filesystem.getFile(packageFile)
 
    if(file.exists()) {
        var text = file.read().text;
        ....
in iOS Simulator it works. But when i try it in Android emulator (2.3.3, V8) the file.exists() function returns false, so i think it is like the file is not found. Even if i comment the if(file.exists()) to avoid checking if the file exists the next code doesn't work (like if it can't find the file).

Can you give me some glue ? There are some differences on how iOS and Android project manages the files inside the Resources folder ?

Thanks!

3 Answers

For file access issues (more common when I need to download a file first) you can use DDMS to browse the emulator/device filessytem to ensure your file was actually copied over. On iOS you can browse the simulators directory structure looking for your files as well.

Now, to your code. Since we don't know what the value of packageFile is, its difficult to troubleshoot.

Take a look at Titanium.Filesystem.resourcesDirectory

packageFile = 'mydata.csv';
var file = Ti.FileSystem.getFile(Titanium.Filesystem.resourcesDirectory + packageFile);

— answered 1 year ago by Stephen Feather
answer permalink
3 Comments
  • (I'm out on the road and can't test it, but I don't remember if Titanium.Filesystem.resourcesDirectory returns with a trailing slash or not, may have to add that)

    — commented 1 year ago by Stephen Feather

  • I'm trying DDMS but i can find where the app is located in the android emulator filesystem. I found that there is a directory named with my bundle id but there are other files inside (a .db file, a "Titanium" file, some .so, etc...). Where can i find the app folder ?

    — commented 1 year ago by Marco Siino

  • Ok now it works: i've put Titanium.Filesystem.resourcesDirectory + ... before the name of the file and now it found it both on iOS and Android. But i don't understand why before it don't found the file only on Android... mistery! Also the documentations says that Titanium.Filesystem.resourcesDirectory is the default path to which the file are opened from when using Ti.Filesystem.getFile. Btw, now it works! Thanks! :)

    — commented 1 year ago by Marco Siino

Right, i've forgot to say what is the value of packageFile. It is "quizzes/indovinelli.qpak" and as said it works on iOS, so if appcelerator manages the paths the same way it should work also on android.

I'll try now seeing with DDMS and let you know.

Thanks in the meanwhile!

Just to be clear: the file indovinelli.qpak is inside Resources/quizzes/indovinelli.qpak in my project folder structure. (in the first question i said quizzes.qpak instead of indovinelli.qpak for simplicity, but the file is indovinelli.qpak).

Your Answer

Think you can help? Login to answer this question!