22

I want to create a new directory inside the SD card programmatically and I want to delete that directory also. How can I do this?

Jason Plank
  • 2,342
  • 4
  • 32
  • 40
James
  • 13,523
  • 26
  • 66
  • 92

6 Answers6

51

To create a directory you can use the following code:

File dir = new File("path/to/your/directory");
try{
  if(dir.mkdir()) {
     System.out.println("Directory created");
  } else {
     System.out.println("Directory is not created");
  }
}catch(Exception e){
  e.printStackTrace();
}

To delete an empty directory, you can use this code:

boolean success = (new File("your/directory/name")).delete();
if (!success) {
   System.out.println("Deletion failed!");
}

To delete a non-empty directory, you can use this code:

public static boolean deleteDir(File dir) {
    if (dir.isDirectory()) {
        String[] children = dir.list();
        for (int i=0; i<children.length; i++) {
            boolean success = deleteDir(new File(dir, children[i]));
            if (!success) {
                return false;
            }
        }
    }

    return dir.delete();
}

Maybe you also need this permission:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

This answer is also a good resource:

How to create directory automatically on SD card

Community
  • 1
  • 1
RoflcoptrException
  • 51,085
  • 34
  • 149
  • 200
24

to create a directory, you could use

File myDirectory = new File(Environment.getExternalStorageDirectory(), "dirName");

if(!myDirectory.exists()) {                                 
  myDirectory.mkdirs();
}

to delete it,

 myDirectory.delete();

dont forget to add permission:

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" 
edwin
  • 7,787
  • 10
  • 48
  • 79
Nakul Sudhakar
  • 1,544
  • 1
  • 25
  • 24
  • 2
    No need to check for directory existence. `mkdirs()` will return true if the directory was created, false on failure or if the directory already existed. – Derek W May 21 '14 at 20:33
4
mkdir() for

File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/abcabcabc"); 
directory.mkdir();


mkdirs() for
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/Mani/abcxyz");
    directory.mkdirs();
MichaelS
  • 5,789
  • 6
  • 29
  • 43
2
new File(Environment.getExternalStorageDirectory(), "DirName").mkdirs();
0

if you want to create root directory and subfolder under it

String root = Environment.getExternalStorageDirectory().toString();

File myDir = new File(root + "/rootfoldername"+"/your sub folder name");

File dir=new File(root + "/rootfoldername"+"/your sub folder name");

myDir.mkdirs();

dir.mkdirs();
Smittey
  • 2,460
  • 10
  • 30
  • 34
vivek pagar
  • 45
  • 1
  • 8
-1

I have created directory and sub directory for my project like this..

        File root = android.os.Environment.getExternalStorageDirectory(); 

        File dir = new File (root.getAbsolutePath() + "/MP3 Music/"); //it is my root directory

        File favourite = new File (root.getAbsolutePath() + "/MP3 Music/" + "Favourites"); // it is my sub folder directory .. it can vary..

        try
        {
             if(dir.exists()==false) 
             {
                     dir.mkdirs();
             }
            /* else
             {
                // Toast.makeText(MainActivity.this, "Root Directory is already exists", Toast.LENGTH_LONG).show();
             }*/

             if(favourite.exists()==false) 
             {
                 favourite.mkdirs();
             }

        }
        catch(Exception e){
          e.printStackTrace();

        }
Zar E Ahmer
  • 32,807
  • 18
  • 222
  • 281