64

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

Alxandr
  • 12,345
  • 10
  • 59
  • 95

6 Answers6

133

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.

zenzelezz
  • 813
  • 2
  • 10
  • 26
synic
  • 26,359
  • 20
  • 111
  • 149
  • 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 '10 at 21:58
  • 3
    Just a note, getExternalStorageDirectory() May not actually point to the physical sd_card, it could point to internal storage. http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory() – Chrispix Apr 27 '12 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 '13 at 15:48
  • This doesn't work for me, since getExternalStorageDirectory() it returns the internal storage. – Luis A. Florit Sep 07 '13 at 01:20
  • There is no "isReadable" method! Unfortunately only a "setReadable" method is available, not even "getReadable"!!! So how we can get this value?! – ABS Apr 26 '14 at 03:24
  • `Might also want to check f.exists(), because if it exists, and isDirectory() returns false, you'll have a problem`? can you please elaborate? why would `isDirectory()` returns **false** when it actually exists? – Muhammad Babar Nov 07 '14 at 07:10
  • 2
    @muhammad: it might exist and just be a file, instead of a directory. – synic Nov 07 '14 at 07:24
46
File dir = new File(Environment.getExternalStorageDirectory() + "/mydirectory");
if(dir.exists() && dir.isDirectory()) {
    // do something here
}
Mark B
  • 183,023
  • 24
  • 297
  • 295
  • 2
    Is it necessary to check both if `exists()` and `isDirectory()` ? I mean what if we use any one of them ? – Mann Jan 04 '16 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();
Artail3
  • 272
  • 3
  • 6
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!!'
}
Ingo
  • 5,239
  • 1
  • 30
  • 24
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().

Shashanth
  • 4,995
  • 7
  • 41
  • 51
  • NOTE: The above directory - http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html no longer exists. – Joe Cullity Jan 11 '14 at 16:54
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();
                }
Agilanbu
  • 2,747
  • 2
  • 28
  • 33