Creating Directories on the SD Card

Quickly share your Android Code Snippets here...

Creating Directories on the SD Card

Postby izu516 » Tue Nov 17, 2009 11:40 pm

I am posting this code (from Kur1310) on how to create directories on the SD Card as it might not be immediately obvious.

Info: value = the folder you are trying to create. (Example: Folder, or Folder/Subfolder)

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. String sdCard = "/sdcard" , value = "";
  3.  
  4. String[] command = { "mkdir",sdCard+"/"+value};
  5.  
  6. try {
  7.  
  8. Process process = Runtime.getRuntime().exec(command);
  9.  
  10.  
  11.  
  12. }catch (IOException e) {
  13.  
  14. Toast.makeText(this, ""+e, Toast.LENGTH_LONG).show();
  15.  
  16. }
  17.  
  18.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
Go anddev!
izu516
Developer
Developer
 
Posts: 27
Joined: Sat Nov 14, 2009 2:18 am
Location: US

Top

Postby divestoclimb » Thu Nov 19, 2009 2:26 am

Another way, all in Java:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. try {
  2.  
  3.         File path = new File("/sdcard/foo/bar");
  4.  
  5.         if(! path.isDirectory()) {
  6.  
  7.                 path.mkdirs();
  8.  
  9.         }
  10.  
  11. } catch(IOException e) {
  12.  
  13.         // ...
  14.  
  15. }
  16.  
  17.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
divestoclimb
Developer
Developer
 
Posts: 33
Joined: Mon May 11, 2009 7:46 pm

Postby izu516 » Thu Nov 19, 2009 2:42 am

Thanks.
Go anddev!
izu516
Developer
Developer
 
Posts: 27
Joined: Sat Nov 14, 2009 2:18 am
Location: US

Postby divestoclimb » Thu Nov 19, 2009 5:29 pm

Some more recommendations on file paths and the SD card:
  • Don't hardcode the path "/sdcard". Instead, use Environment.getExternalStorageDirectory()
  • Don't hardcode "/" characters in your paths. For maximum compatibility of your code on non-Android platforms, use File.separator.
  • Instead of concatenating strings and separators to make paths, the cleanest method is to build File objects using the different constructors available like File(String dirPath, String name) or File(Uri uri).
divestoclimb - Android SCUBA app developer
Android Community | Gas Mixer, ScubaLib, Scuba Tanks | d2c Checklist
All code is open source under Apache license.
divestoclimb
Developer
Developer
 
Posts: 33
Joined: Mon May 11, 2009 7:46 pm

Top

Return to Code Snippets for Android

Who is online

Users browsing this forum: No registered users and 6 guests