Android: mkdirs()/mkdir() on external storage returns false

MHM picture MHM · Aug 27, 2011 · Viewed 42.3k times · Source

I'm driven crazy with this:

Log.d("STATE", Environment.getExternalStorageState());
File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "work_data");
Log.d("PATH", f.getAbsolutePath());
if (!f.exists()) {
    Log.d("MAKE DIR", f.mkdirs() + "");
}

The output log looks like this:

STATE     mounted
PATH      /mnt/sdcard/DCIM/work_data
MAKE DIR  false

I made sure to add the correct permission:

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

But I don't know why it could not create the folder. I also used mkdir() step by step but the result is the same. Please help me. I have googled so much and spent at least 2 days on this stupid thing. Thanks for your help!!

EDITING:

Sorry everyone! I had added <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> in <application> tag. this was my mistake! But thank you all for reply.

Answer

Fido Zhang picture Fido Zhang · Jun 13, 2016

I got the same problem,and I am sure I had put the permission tag in the right place,but mkdirs didn't work yet, my system is Android 6.0, I resolve it now , you can check as below:

  1. make sure your put the permission tag in .
  2. open "setting/application" in your phone,check your application's permission(I found the permission in my manifest statement is not here),open the switch of the permission like this.(I found it is closed in default which make "mkdirs" failed)

enter image description here