Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mkdirs aways return false in Android 5.1

I searched these issues, but couldn't resolve it.Please help me in solving this problem

That 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 device android version is: 5.1.1, 12GB free space, and the code run regular under 5.0

While running the app I encountered this Exception :

android.system.ErrnoException: mkdir failed: EACCES (Permission denied) when I debug step into File.mkdirs
like image 662
Eraise Avatar asked Apr 26 '16 01:04

Eraise


3 Answers

OP remembered to add the exception to his/her question. This answer is not relevant anymore.

--

You are not giving it a new folder to create. Try this instead:

...
File _dirPath = new File(_sdcardPath + "/CreateFolder");
...
like image 104
muratgu Avatar answered Nov 10 '22 04:11

muratgu


I found nothing is wrong with your code. Based on mkdir() and this file doc, I think the folder existed.

false on failure or if the directory already existed.

It means the following code:

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

only attempt to (re)create the existing folder /storage/emulate/0, thus, returned false.

To create new folder, try this:

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

See more

like image 2
T D Nguyen Avatar answered Nov 10 '22 03:11

T D Nguyen


Please check DocumentFile for accessing SDcard from android 4.4 and above. More detail in this link

like image 1
NamNH Avatar answered Nov 10 '22 03:11

NamNH