65

How do I check if a directory exist on the sdcard in android?

6 Answers 6

134

Regular Java file IO:

File f = new File(Environment.getExternalStorageDirectory() + "/somedir");
if(f.isDirectory()) {
   ....

Might also want to check f.exists(), because if it exists, and isDirectory() returns false, you'll have a problem. There's also isReadable()...

Check here for more methods you might find useful.

7
  • My error just turned out to be a lacking directory in my path, but thanks. I began to wonder if android's filesystem didn't support normal methods like this :-P
    – Alxandr
    Apr 12, 2010 at 21:58
  • 3
    Just a note, getExternalStorageDirectory() May not actually point to the physical sd_card, it could point to internal storage. developer.android.com/reference/android/os/…
    – Chrispix
    Apr 27, 2012 at 18:29
  • 1
    Chrispix, your comment is misleading. getExternalStorageDirectory() will point to the storage which shares data across all applications. This could be the SD Card, and for devices without SD Cards it points to the reserved storage area that is used as external storage. Internal storage is where private data (to the application - usually data/data) is stored.
    – Yash
    Jul 20, 2013 at 15:48
  • This doesn't work for me, since getExternalStorageDirectory() it returns the internal storage. Sep 7, 2013 at 1:20
  • 2
    @muhammad: it might exist and just be a file, instead of a directory.
    – synic
    Nov 7, 2014 at 7:24
46
File dir = new File(Environment.getExternalStorageDirectory() + "/mydirectory");
if(dir.exists() && dir.isDirectory()) {
    // do something here
}
1
  • 2
    Is it necessary to check both if exists() and isDirectory() ? I mean what if we use any one of them ?
    – Mann
    Jan 4, 2016 at 11:22
17

The following code also works for java files:

// Create file upload directory if it doesn't exist    
if (!sdcarddir.exists())
   sdcarddir.mkdir();
2

General use this function for checking is a Dir exists:

public boolean dir_exists(String dir_path)
  {
    boolean ret = false;
    File dir = new File(dir_path);
    if(dir.exists() && dir.isDirectory())
      ret = true;
    return ret;
  }

Use the Function like:

String dir_path = Environment.getExternalStorageDirectory() + "//mydirectory//";

if (!dir_exists(dir_path)){
  File directory = new File(dir_path); 
  directory.mkdirs(); 
}

if (dir_exists(dir_path)){
  // 'Dir exists'
}else{
// Display Errormessage 'Dir could not creat!!'
}
1

I've made my mistake about checking file/ directory. Indeed, you just need to call isFile() or isDirectory(). Here is the docs

You don't need to call exists() if you ever call isFile() or isDirectory().

1
0

Yup tried a lot, beneath code helps me :)

 File folder = new File(Environment.getExternalStorageDirectory() + File.separator + "ur directory name");

                if (!folder.exists()) {
                    Log.e("Not Found Dir", "Not Found Dir  ");
                } else {
                    Log.e("Found Dir", "Found Dir  " );
                   Toast.makeText(getApplicationContext(),"Directory is already exist" ,Toast.LENGTH_SHORT).show();
                }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.