views:

102

answers:

4

hello guyz i have been trying to create a directory in sdcard programatically but it always showing me directory not created.

my code is this.

     boolean success = (new File("/sdcard/map")).mkdir(); 
     if (!success) 
     { 
         // Directory creation failed } 
         Log.i("directory not created", "directory not created");

     }
     else 
     {
         Log.i("directory is created", "directory is created"); 
     }
A: 

Do you have the right permissions to write to SD card in your manifest ? Look for WRITE_EXTERNAL_STORAGE at http://developer.android.com/reference/android/Manifest.permission.html

Matthieu
yes i have added this line in manifest file in application tag but still it does not creating the directory.
sajjoo
A: 

Isn't it already created ? Mkdir returns false if the folder already exists too mkdir

fedj
+3  A: 

Hi Sajjoo,

There are three things to consider here.

  1. Don't assume that the sd card is mounted at /sdcard (May be true in the default cases but better not to hard code). You can get the location of sdcard by querying the system.

    Environment.getExternalStorageDirectory()

  2. You have to inform Android that your application needs to write to external storage by adding uses-permission entry in the AndroidManifest.xml file.

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    
  3. If this directory already exists, then also mkdir is going to return false. So, you check for the existence of the directory and then try creating it if it does not exist. In your component use something like below.

    File folder = new File(Environment.getExternalStorageDirectory() + "/map");
    boolean success = false;
    if(!folder.exists())
    {
        success = folder.mkdir();
    }         
    if (!success) 
    { 
        // Do something on success
    }
    else 
    {
        // Do something else on failure 
    }
    

And don't forget to include the below line in AndroidManifest.xml.

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

Hope this helps.

Gopinath
Before Froyo, SD card was mounted at /sdcard. From Froyo onwards, the sd card is mounted at /mnt/sdcard and a soft link is created to /sdcard so that applications will still behave properly despite the change in mount point.
Gopinath
A: 

The correct path to the sdcard is

/mnt/sdcard/

but, as answered before, you shouldn't hardcode it. If you are on Android 2.1 or after, use

getExternalFilesDir() 

Otherwise:

Environment.getExternalStorageDirectory()

Read carefully http://developer.android.com/intl/fr/guide/topics/data/data-storage.html#filesExternal

Also, you'll need to use this method or something similar

boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();

if (Environment.MEDIA_MOUNTED.equals(state)) {
    // We can read and write the media
    mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
    // We can only read the media
    mExternalStorageAvailable = true;
    mExternalStorageWriteable = false;
} else {
    // Something else is wrong. It may be one of many other states, but all we need
    //  to know is we can neither read nor write
    mExternalStorageAvailable = mExternalStorageWriteable = false;
}

then check if you can access the sdcard. As said, read the official documentation.

Another option, maybe you need to use mkdirs instead of mkdir

file.mkdirs()

Creates the directory named by the trailing filename of this file, including the complete directory path required to create this directory.

Maragues
@Maragues: There is no `getExternalFilesDir()` on `Environment` in any version of Android. You may be thinking of `getExternalFilesDir()` on `Context`, which was added in 2.2. However, that method is somewhat broken -- the directory it gives you will have its contents deleted during an app upgrade. I hope this will be fixed in Gingerbread.
CommonsWare
Thx, fixed. I quickly browsed through the data storage section and assumed it was accessed just as getExternalStorageDirectory(), which is what I use.
Maragues