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?
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.