Mkdirs aways returns false in Android 5.1

I looked for these problems, but could not solve it. Please help me in solving this problem.

This is my mkdir code:

File _sdcardPath = Environment.getExternalStorageDirectory(); // sdcard path is /storage/emulate/0
File _dirPath = new File(_sdcardPath, "CreateFolder");
boolean _isCreate = _dirPath.mkdir();
if (_isCreate) {
   tvResult.append(_dirPath + " mkdir success");
} else {
   tvResult.append(_dirPath + " mkdir fail");
}

my manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.createfolder"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <uses-sdk
        android:minSdkVersion="11"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

My version of the Android device: 5.1.1, 12 GB free space and regular code execution under 5.0

When starting the application, I encountered this Exception:

android.system.ErrnoException: mkdir failed: EACCES (Permission denied) when I debug step into File.mkdirs
+4
source share
4 answers

Please check DocumentFile for access to SD card with Android 4.4 and above. More in this link

+1
source

OP remembered adding an exception to his question. This answer is no longer relevant.

-

You do not create a new folder to create. Try instead:

...
File _dirPath = new File(_sdcardPath + "/CreateFolder");
...
+3

. mkdir() doc, , .

false .

:

File _dirPath = new File(_sdcardPath, "CreateFolder");
boolean _isCreate = _dirPath.mkdir();

() /storage/emulate/0, , false.

, :

File _dirPath = new File(_sdcardPath + "/CreateFolder");
boolean _isCreate = _dirPath.mkdir();// this will create folder CreateFolder

+2

, /storage/emulate/0/CreateFolder mkdir:

File _sdcardPath = Environment.getExternalStorageDirectory();
File _dirPath = new File(_sdcardPath, "CreateFolder");
final boolean dirExisted = _dirPath.exists();
if (dirExisted && _dirPath.isFile()) {
    _dirPath.delete();
}
if (dirExisted) {
    tvResult.append(_dirPath + " dir existed");
    return;
}
boolean _isCreate = _dirPath.mkdir();
if (_isCreate) {
    tvResult.append(_dirPath + " mkdir success");
} else {
    tvResult.append(_dirPath + " mkdir fail");
}

file, delete it.If dir, .

+1

All Articles