Titanium Community Questions & Answer Archive

We felt that 6+ years of knowledge should not die so this is the Titanium Community Questions & Answer Archive

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

— asked April 13th 2011 by Brian Dittmer
  • android
  • filesystem
  • foldercreation
0 Comments

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.

    — answered December 5th 2011 by Xavier Balderas
    permalink
    0 Comments
  • 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

    — answered April 13th 2011 by Kosso
    permalink
    1 Comment
    • 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 ?

      — commented April 14th 2011 by Abdul Rehman
  • 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 ?

    — answered April 14th 2011 by Abdul Rehman
    permalink
    3 Comments
    • hmm.. btw, I just updated my code below to check to make sure the folder doesn't already exist before creating it. Are you sure it's not already there?

      If not, I can't think what else it might be. ;/ I wonder if it's even possible to 'write' to the external storage. I know that you can't write to the Resources folder, but you can to the application data directory ('Documents').

      Is the SD card to be writable? (with the little plastic switch?)

      Sorry, I don't have an android device to hand to test it out.

      — commented April 14th 2011 by Kosso
    • Out of interest, does the value of Titanium.Filesystem.externalStorageDirectory return the path including the com.yourdomain.appname part at the end?

      — commented April 14th 2011 by Kosso
    • one more thought: after installing your app to the device, have you turned 'usb storage' off? (to unmount the sd card from your pc)

      — commented April 14th 2011 by Kosso
  • 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.

    — answered April 14th 2011 by Kosso
    permalink
    1 Comment
    • i have to test for Sony Xperia Z but Not start emulator if i give manual size …. is there any changes required to do in SD card ? or what
      to do?

      — commented June 28th 2013 by Jayesh Joshi
The ownership of individual contributions to this community generated content is retained by the authors of their contributions.
All trademarks remain the property of the respective owner.