0

I am trying to create a folder with mkdir() with an app on a Google Nexus 7 tablet. After that I am saving an image in that folder.

The relevant code is:

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdir()) {
            Log.d(TAG, "Oops! Failed create "
                    + Config.IMAGE_DIRECTORY_NAME + " directory");
            return null;
        }
    }

and the log says: "Oops! Failed create userImages".

What I have tried so far:

  • testing the App on a Samsung Galaxy S3 mini with Android 4.1.2 and it worked. (-> it seems to be device specific)

  • Adding the permission
    < uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" >
    to the AndroidManifest.xml

  • using mkdirs() instead of mkdir()

  • the function

    //Check SD card state
    String state = Environment.getExternalStorageState();
    if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state) || !Environment.MEDIA_MOUNTED.equals(state)) {
        Log.e(TAG, "Error: external storage is read only or unavailable");
    } else {
        Log.d(TAG, "External storage is not read only or unavailable");
    }
    

    gives "External storage is not read only or unavailable".

My code:

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="de.hbksaar.bubbles">
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest>

Does anybody have a clue, why it is not possible for me to create that folder?

2 Answers2

0

if the android version is marshmallow then the permission check is dynamic.

Please use ContextCompat to chek permission.

peeyush pathak
  • 3,663
  • 3
  • 19
  • 25
  • Checked it with "ContextCompat.checkSelfPermission(this, permission);" and the result was PERMISSION_DENIED. Why is the static permission != the runtime permission? What can I do? – Julian Haluska Jan 17 '16 at 16:59
  • check this out. http://developer.android.com/training/permissions/requesting.html – peeyush pathak Jan 17 '16 at 17:29
0

Which version of Android OS you used ? If you run on Android 6.0, it maybe due to you app donot have Storage permission, go to Settings -> Apps -> Your App -> Permissions to double check your app have Storage permission. Grant Storage permission if not granted yet.

Try to log the full path of mediaStorageDir to make sure it is a correct path on SD Card.

Also try to use mkdirs() to create non-exist parent directories.

Ming C
  • 2,476
  • 2
  • 14
  • 8
  • When I change the permission settings it does work! Thanks! But I want to have the permission to write by default, e.g. when installing the app or at least the user should be asked, when I am trying to write, how is this possible? – Julian Haluska Jan 17 '16 at 17:11
  • checkout request permission at run time: http://developer.android.com/training/permissions/requesting.html – Ming C Jan 17 '16 at 17:18