Android: Mkdirs()/Mkdir() on External Storage Returns False

Android: mkdirs()/mkdir() on external storage returns false

I have had the same problem and I have searched everything for a week trying to find the answer. I think I found it and I think it's ridiculously easy, you have to put the uses-permission statement in the right place...

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.company.name"
android:versionCode="1"
android:versionName="0.2">

<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

When I had it inside the <application></application> section it did not work.

Android mkdir() returns false, doesn't create folder

Creating external storage directories requires the WRITE_EXTERNAL_STORAGE permission; without this permission, attempts to write external storage will fail. Also, not all directories are necessarily writable; use getExternalStoragePublicDirectory() to get a directory that has shared write access (other external directories may be read-only). However, if the purpose of writing to external storage is to share the file with other applications, you should strongly consider using the FileProvider API, instead (see also Setting Up File Sharing); with this strategy, your app stores files in its own, internal app-specific directories, but then enables selective sharing of these files to other apps through a content provider. This strategy provides greater security for the files and also makes it possible for you to provide greater access control over reading/writing of the files.

Since you say that you already have the required permission*, most likely where you are getting things wrong is in the call to toString(). There is no guarantee that the toString() method on a file returns its full path. Use getPath() or getAbsolutePath() when concatenating these. It is also advisable, when choosing to write to external storage, that you first check its state; the external storage can in some cases be ejected/unmounted and not available.

*You should verify this with ContextCompat.checkSelfPermission(). You should add a call to requestPermissions() if the permissions are not present.

Android mkdirs() return false on Android 11 with Environment.getExternalStorageDirectory()

Why android:requestLegacyExternalStorage="true" is not working?

android:requestLegacyExternalStorage is no longer works on Android 11+. It is just a helper on Android 10 to give developers more time before migrating to scoped storage. On scoped storage, you need to use URI for creating, renaming, moving files, etc. Thus java.io.File is almost useless now.

Is it confirmed that in Android 11 we can't create any folder on the root directory? any workaround?

No workaround.

BTW, to reduce scoped storage complexity, I've created a library named Simple Storage. It works across API levels.

Android Q: file.mkdirs() returns false

There was huge privacy change in android Q by introducing Scoped Storage.

Since Q beta 4 it's possible to opt-out of that feature by:

  • targeting API 28 (or lower)
  • using requestLegacyExternalStorage manifest attribute (while targetting API 29):


<manifest ... >
<!-- This attribute is "false" by default on apps targeting Android Q. -->
<application android:requestLegacyExternalStorage="true" ... >
...
</application>
</manifest>

edit: as mentioned in other answer this does not work if app is targeting API 30 - Android 11 devices will ignore legacy storage flag.

edit 2: heads up for anyone planning to publish on play store - soon usage of this flag will be restricted (new and updated apps won't be accepted) unless its required for core functionality (e.g. file manager)

android mkdirs in SD card return false.help me, it spends me 3days

Try this. It might help you.

String fullPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/happydiarybackup/";
try
{
File dir = new File(fullPath);
if (!dir.exists()) {
dir.mkdirs();
}
}
catch (Exception e) {
Log.e("App", "Exception" + e.getMessage());
}


Related Topics



Leave a reply



Submit