user5551022
user5551022

Reputation: 55

mkdirs() return false with write external permission

I've a problem with mkdirs function. See my permissions manifest

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="xxxxxx"
android:versionCode="15"
android:versionName="3.1.1508">

<!-- Global permissions -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

And my code

String APP_PATH_SD_CARD = "/xxxxx";
    String fullPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + APP_PATH_SD_CARD;

    if(!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())){
        ToastDressmup.make(this, "External SD card not mounted", 0, ToastAction.CLOSE.getValue()).show();
    }

    try {
        File dir = new File(fullPath);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        ==> My file doesn't exist

SD card is mounted. I don't understand. I read very answers on internet, but it's still wrong.

I was tested on emulator (with emulated sdcard) and on Nexus 5, it's doesn't work. On my HTC One X it's work, why ?! I'me desperate

Thank you for all! G.

Upvotes: 1

Views: 1152

Answers (1)

Denis Lukenich
Denis Lukenich

Reputation: 3164

As solved in the comments, the answer is that the permission was missing.

Since Android 6 the permission handling has changed as stated:

Exception 'open failed: EACCES (Permission denied)' on Android

or

Android permission doesn't work even if I have declared it

You need to gather these permissions at runtime again:

Quote from first answer:

/**
 * Checks if the app has permission to write to device storage
 *
 * If the app does not has permission then the user will be prompted to grant permissions
 *
 * @param activity
 */
public static void verifyStoragePermissions(Activity activity) {
    // Check if we have write permission
    int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);

    if (permission != PackageManager.PERMISSION_GRANTED) {
        // We don't have permission so prompt the user
        ActivityCompat.requestPermissions(
                activity,
                PERMISSIONS_STORAGE,
                REQUEST_EXTERNAL_STORAGE
        );
    }
}

Upvotes: 1

Related Questions