5

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
Lips_coder
  • 686
  • 1
  • 5
  • 17
Eraise
  • 163
  • 12
  • what is the new folder that you want to create? as it is, there is nothing to create so it returns false. – muratgu Apr 26 '16 at 02:12
  • 1
    @muratgu No, "CreateFolder" not a dir or file in storage, I make sure via adb shell – Eraise Apr 26 '16 at 02:37

4 Answers4

3

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");
...
muratgu
  • 7,241
  • 3
  • 24
  • 26
2

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

Community
  • 1
  • 1
T D Nguyen
  • 7,054
  • 4
  • 51
  • 71
1

check if /storage/emulate/0/CreateFolder exists before mkdir like this:

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");
}

If the path exists a file, try to delete it.If is a dir, no need to create it anymore.

hyongbai
  • 244
  • 1
  • 8
  • "CreateFolder" is a new name of my storage, my storage not have any file or dir name "createFolder" – Eraise Apr 26 '16 at 02:30
1

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

Community
  • 1
  • 1
NamNH
  • 1,752
  • 1
  • 15
  • 37
  • I'm fail to create create directory via run this statement : DocumentFile.fromFile(_sdcardPath).createDirectory("CreateFolder"); – Eraise Apr 26 '16 at 05:43
  • DocumentFile.fromFile(_sdcardPath).canWrite() return true – Eraise Apr 26 '16 at 06:00
  • Take time to read the document links and find the examples code for it. There are some APIs to take the access sdcard permission. I don't have experiences on it, but I am sure that is solution you need. – NamNH Apr 26 '16 at 06:47
  • Thank for your answer, I try like https://developer.android.com/guide/topics/providers/document-provider.html#create, but the devices only start explorer, so I couldn't create a directory or file. – Eraise Apr 26 '16 at 07:22
  • Yeah, that you go through half of road. When it starts the explorer, user will grant your app the access storage (sd) permission. Then on activityForResult do with the result code [like this](https://developer.android.com/guide/topics/providers/document-provider.html#results). – NamNH Apr 26 '16 at 10:15
  • But I want to save a bitmap on my app, can I do it on background? – Eraise Apr 27 '16 at 02:21
  • I am afraid that you can not do it without getting permission from user. So, I think you need take permission in the first time run app, if user granted it, continue your app, otherwise finish your app and show it in the next time. – NamNH Apr 27 '16 at 03:46
  • You should read my answer related links. The reading, writing SD files process was changed and You know that is a security feature on new Android versions. I think you can not find any other solutions without taking permission from user. I suggest you 2 options:1:Write on internal storage, 2:Take permission at first time lauch app. – NamNH Apr 27 '16 at 08:40
  • I write cache on Context.getCacheDir, and set MemoryPersistence to MQTTAndroidClient. And I read these links, good data, thank you. – Eraise Apr 29 '16 at 06:05
  • There are some risks on this folder as its docs. Be careful. Anyway, do what you want that make it works. – NamNH Apr 29 '16 at 07:45