1

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.

user5551022
  • 55
  • 10
  • Possible duplicate of [mkdirs returns false for directory on sd card while the parent directory is writable](http://stackoverflow.com/questions/4062357/mkdirs-returns-false-for-directory-on-sd-card-while-the-parent-directory-is-writ) – Henry Nov 11 '15 at 15:04
  • Thx! But their solutions do not fix my problem :( I was tested mkdirs(), mkdir on each directory, with emulator or without. I haven't more ideas :( – user5551022 Nov 11 '15 at 15:15
  • If you are using Android6, check the answer of easyspeek here. The permission handling has changed. – Denis Lukenich Nov 11 '15 at 15:17
  • https://stackoverflow.com/questions/32635704/cant-get-the-permission – CommonsWare Nov 11 '15 at 15:22
  • My link was missing: http://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android/33292700#33292700 – Denis Lukenich Nov 11 '15 at 15:25
  • Waouuuu, you're right @DenisLukenich! With previous API is good. I will search the true permission now ;) Thank you very much – user5551022 Nov 11 '15 at 15:25

1 Answers1

1

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
        );
    }
}
Community
  • 1
  • 1
Denis Lukenich
  • 3,084
  • 1
  • 20
  • 38