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
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");
...
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
Please check DocumentFile for accessing SDcard from android 4.4 and above. More detail in this link
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With