Check if directory exist on android's sdcard

AndroidDirectoryAndroid Sdcard

Android Problem Overview


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

Android Solutions


Solution 1 - Android

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.

Solution 2 - Android

File dir = new File(Environment.getExternalStorageDirectory() + "/mydirectory");
if(dir.exists() && dir.isDirectory()) {
    // do something here
}

Solution 3 - Android

The following code also works for java files:

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

Solution 4 - Android

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!!'
}

Solution 5 - Android

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().

Solution 6 - Android

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();
                }

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlxandrView Question on Stackoverflow
Solution 1 - AndroidsynicView Answer on Stackoverflow
Solution 2 - AndroidMark BView Answer on Stackoverflow
Solution 3 - AndroidArtail3View Answer on Stackoverflow
Solution 4 - AndroidIngoView Answer on Stackoverflow
Solution 5 - Androiduser942821View Answer on Stackoverflow
Solution 6 - AndroidAgilanbuView Answer on Stackoverflow