1

This is my code:

boolean success = false;
Log.d(TAG, Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_PICTURES + "/myFolder" );
myFolder = new File( Environment.getExternalStorageDirectory() + "/" + Environment.DIRECTORY_PICTURES + "/myFolder" );

if( myFolder.exists() ){
    Log.d(TAG, "FOLDER EXISTS");
}else{

    success = myFolder.mkdir();

    if( success ){
        // Do something on success

    } else {
        // Do something else on failure 
        throw new RuntimeException("File Error in writing new folder");
}

What is wrong?

Running it on HTC Desire HD - android 2.3.5 will cause this output:

D/DVA_HLUI Application(25723): /mnt/sdcard/Pictures/myFolder
E/AndroidRuntime(25723): FATAL EXCEPTION: main
E/AndroidRuntime(25723): java.lang.RuntimeException: Unable to create application com.DVA_HLUI.DVAHLUI_App: java.lang.RuntimeException: File Error in writing new folder
E/AndroidRuntime(25723):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3920)
E/AndroidRuntime(25723):    at android.app.ActivityThread.access$2200(ActivityThread.java:135)
E/AndroidRuntime(25723):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1098)
E/AndroidRuntime(25723):    at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(25723):    at android.os.Looper.loop(Looper.java:150)
E/AndroidRuntime(25723):    at android.app.ActivityThread.main(ActivityThread.java:4385)
E/AndroidRuntime(25723):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(25723):    at java.lang.reflect.Method.invoke(Method.java:507)
E/AndroidRuntime(25723):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:849)
E/AndroidRuntime(25723):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:607)
E/AndroidRuntime(25723):    at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(25723): Caused by: java.lang.RuntimeException: File Error in writing new folder
E/AndroidRuntime(25723):    at com.DVA_HLUI.DVAHLUI_App.onCreate(DVAHLUI_App.java:185)
E/AndroidRuntime(25723):    at android.app.Instrumentation.callApplicationOnCreate(Instrumentation.java:984)
E/AndroidRuntime(25723):    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3917)
E/AndroidRuntime(25723):    ... 10 more
Matteo
  • 7,924
  • 24
  • 84
  • 129

2 Answers2

5

I'm not sure just try to create folder like below -

success = myFolder.mkdirs();

instead of

success = myFolder.mkdir();

as per mkdirs() And, don't forget to add the below permisstion into your manifest file.

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

Also have a look at this answer - How to create directory automatically on SD card

Community
  • 1
  • 1
Praveenkumar
  • 24,084
  • 23
  • 95
  • 173
  • Hey, thks for answering, and sorry for delay in response. `mkdirs()` was the correct indication! turns out that `mkdirs()` creates all directories in the path while `mkdir()` won't! – Matteo Sep 27 '12 at 14:12
0

You should Use This Permission in your mainfest file.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Rishabh Agrawal
  • 861
  • 2
  • 15
  • 25