Create folder on SDCARD - Android
how do we go about creating a folder on the Android SDCard?
looking for something like /SDCARD/OURAPPName/
Is it possible to do it when the app is installed?? (like you see with other apps like Dropbox etc)
thanks
~Brian
4 Answers
-
If you want to create a folder directly in the sdcard root (not inside appdata), you can use this code:
var _storage = Ti.Filesystem.applicationDataDirectory; if(Ti.Filesystem.isExternalStoragePresent()){ _storage = 'file:///sdcard/'; }; var folder = Ti.Filesystem.getFile(_storage,'folder_name'); if (!folder.exists()){ folder.createDirectory(); }
For me works.
-
Hi,
Try this:
var new_folder_name = 'CREATED_BY_TITANIUM'; // check to see if we have external storage present if(Titanium.Filesystem.isExternalStoragePresent()){ var sd_card_path = Titanium.Filesystem.externalStorageDirectory; // this will create the new folder on the sd card and return a filesystem object var new_folder = Titanium.Filesystem.getFile(sd_card_path, new_folder_name); if(!new_folder.exists()){ new_folder.createDirectory(); } // this bit didnt work.. Ti.API.info('New folder is at path : '+new_folder.nativePath()); }
hope this helps
-
I tried creating folder on SD card but the following code doesn't work.
var new_folder_name = 'XA365'; var newDir = Titanium.Filesystem.getFile(Titanium.Filesystem.externalStorageDirectory,new_folder_name); newDir.createDirectory();
I have necessary permission set in the xml file. Are there any further steps that I need to follow ?
-
OK.. I just dug out my old Nexus One and tried the code below which worked fine. It created a folder called 'CREATED_BY_TITANIUM' on the SD card within the folder called 'com.mydomain.myappname' :
var new_folder_name = 'CREATED_BY_TITANIUM'; // check to see if we have external storage present if(Titanium.Filesystem.isExternalStoragePresent()){ var sd_card_path = Titanium.Filesystem.externalStorageDirectory; // this will create the new folder on the sd card and return a filesystem object var new_folder = Titanium.Filesystem.getFile(sd_card_path, new_folder_name); if(!new_folder.exists()){ new_folder.createDirectory(); } }
One thing I did notice was that it didn't work if I tried to get the nativePath() of the new file/folder object. The app hung. So guess that's a bug.
It seems that on Android the Titanium.Filesystem.externalStorageDirectory is actually returned as appdata:// , instead of an actual path, which I assume breaks the 'nativePath()' method.
You should be able to write files to it, list files etc by using the 'new_folder' file object as normal though.
hope this helps.