Android: mkdirs()/mkdir() on external storage returns false
Solution 1
I have had the same problem and I have searched everything for a week trying to find the answer. I think I found it and I think it's ridiculously easy, you have to put the uses-permission statement in the right place...
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.name"
android:versionCode="1"
android:versionName="0.2">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
When I had it inside the <application></application> section it did not work.
Solution 2
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:
- make sure your put the permission tag in .
- 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)
Solution 3
I know this is an old post but perhaps my answer can help somebody.
After several days dealing with this problem I have realized that while the phone is connected to the PC, which it turns to be always during development, the SD card is not available. Thus making to fail any attempt to create a directory or file over it. To make it "easier" it seemed to behave differently depending of the telephone under test.
I know it can sound quite a silly problem, but it cost a lot of time that maybe some other can save.
Solution 4
add this line of code in OnCreate()
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},1);
Solution 5
Applicable only if your targetSdkVersion is 29 or above
Environment.getExternalStorageDirectory() is deprecated in API level 29.
To get the same functionality use this line
File mediaStorageDir = context.getExternalFilesDir(null);
If you have checked all the possible errors then try this fix.
Already answered here - https://stackoverflow.com/a/61480077/7764015
![MHM](https://i.stack.imgur.com/diTaS.jpg?s=256&g=1)
MHM
Updated on July 09, 2022Comments
-
MHM almost 2 years
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. -
MHM almost 13 yearsNo. it doesn't. I check it in line
if (!f.exists()) {
-
MHM almost 13 yearsI checked
f.mkdir()
too but there is no difference. I also check in files System and there is not such folder!!! -
Dharmendra almost 13 yearsI had just copy and paste your code in my activity and it is working it is creating folder.I don't know why you are getting problem.
-
Hamidreza Sadegh almost 8 yearsmaybe in some device, not all of them
-
Chris almost 8 yearsIn Android 6.0, you request certain permissions when you use them. You can make the request using the function
ActivityCompat.requestPermissions
. See here for more information. -
Pham Hung about 7 yearsCorrect answer in my situation. Vote it up
-
jiwopene almost 6 yearsHidden directory name always starts with
.
(dot). -
Sam over 5 yearsI have the correct permission in
Manifest
file, and it works fine on android 7.1, but tried on android 9.0 not working,requestPermission
did the trick, thx!! -
alex_z over 5 yearsThis sorted my issue with API 26+
-
claudevandort over 5 yearsIn my case I was asking for permissions in the manifest, but somehow it was disabled in App permissions. You saved my life, I love youu! ❤❤❤
-
Randy almost 5 yearsFor some reason, I just had to toggle this permission off then on again and it worked.
-
KisnardOnline almost 4 yearsBless your heart! Not sure how I missed this or why Android Studio didn't forcibly warn me that was deprecated. Seems like a big deal to remove that.
-
Fisher almost 4 yearsFor targetSdkVersion 29, there may be some differences. getExternalFilesDir(null) is the directory of the app which is created in .../Android/data/ with app-package-name. If someone's old app created some directory in other place, such like in root. This answer cannot cover the issue because they are two different directories. A temporary solution should be this: developer.android.com/training/data-storage/… <application android:requestLegacyExternalStorage="true" ... >
-
BDL over 3 yearsAlthough this code might solve the problem, a good answer should also explain what the code does and how it helps.
-
Kalyan Pradhan over 3 yearsThis one fixed my issue too,, I am using targetSdkVersion greater than 29
-
Khay Leng about 3 yearsthen how to test mkdir() with my phone connected??
-
Johann over 2 yearsNope. Didn't work for me.
-
Johann over 2 yearsgetExternalFilesDir will only return access to the app's own private data on the external storage. These files/folders are not accessible to other apps.
-
Johann over 2 yearsDidn't work for me.