Read file from sdcard

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

Hi, I would like to read files from the sdcard of a device. How I can do that? I've tried to use:

var myFile = Titanium.Filesystem.getFile(Titanium.Filesystem.separator + 'sdcard' + Titanium.Filesystem.separator + 'file.txt');
var myFile = Titanium.Filesystem.getFile('/sdcard/file.txt');
var myFile = Titanium.Filesystem.getFile('file.txt');
var myFile = Titanium.Filesystem.getFile(Titanium.Filesystem.applicationDataDirectory + 'file.txt');
After this, I use adb for uploading the file to the device (in this case the emulator). I can't locate the file...

Another thing I've tried is to show through Titanium.API.info the nativePath of the file that I would like to read and "push" it in that path..It doesn't work!

The only things that works is

var myFile = Titanium.Filesystem.getFile(Titanium.Filesystem.resourcesDirectory, 'file.txt');
putting the file in the directory resources of the project. The question is: How I can read a file not included in the project (for example in the sdcard or other storage places accesible from the users)? What is the correct path to using Titanium.Filesystem.getFile()? I approached to Titanium 3 weeks ago and this is the only thing I'm not able to do (for the moment :-) ) Thanks

1 Answer

Using the following returns the folder, named as your Application ID (something like com.mysite.myapplication), inside the sdcard directory:

var myAppDir = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory);
Then you can use the getParent function to obtain the sdcard directory:
var sdcardDir = myAppDir.getParent();
Now you can navigate to any other existing directories and files not associated with your project.

Blessings!

— answered 2 years ago by Eric Whitlock
answer permalink
1 Comment
  • sdcardDir returns null to me, and how can i specify exact file in the SD to read from it

    — commented 1 year ago by Sara Saied

Your Answer

Think you can help? Login to answer this question!