android – mkdirs returns false for directory on sd card while the parent directory is writable-ThrowExceptions

Exception or error:

When starting my android application, I need to create a directory on the sd card, for a small number of users this fails and I can’t figure out the reason for it…

(I’ve found similar problems caused by the WRITE_EXTERNAL_STORAGE permission missing, it’s there and it works for almost all users so I don’t think this is reason)

I’ve simplified the previous situation to make it easier to explain, if creating a directoy fails, I run a test case where I try to make a .test directory on the sdcard:

new File(Environment.getExternalStorageDirectory(), ".test").mkdir() -> false
new File(Environment.getExternalStorageDirectory(), ".test").mkdirs() -> false

File properties of the relevant directories:

/sdcard/.test (exists=false canWrite=false canRead=false canExecute=err isDirectory=false isFile=false)

/sdcard (exists=true canWrite=true canRead=true canExecute=err isDirectory=true isFile=false)

/ (exists=true canWrite=false canRead=true canExecute=err isDirectory=true isFile=false)

getExternalStorageState=mounted

(canExecute returns err because the test is run on sdk < 9)

Suggestions and ideas are very welcome…

How to solve:

It is common when you don’t have a permission in your manifest.

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

As for me it was the only wrong thing.

###

First of all, technically mkdirs() returning false doesn’t mean that it failed, it just meant that it didn’t make the directories.
If the directories already exist then mkdirs() will continue to return false.

Secondly, in Android 6, not only do you need:

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

You also need some equivalent of:

ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED

For more info on checking permissions, see:
https://github.com/googlesamples/android-RuntimePermissions

###

I had a simillar problem and spent several hours to realise what is wrong. It didn’t work on Sumsung Mega, but for other phones it worked fine.
I really had WRITE_EXTERNAL_STORAGE permission and getExternalStorageDirectory is mounted and available. But still the directory wasn’t created.
What helped ?
I just restarted the divice! And it helped!
It’s a pity that nobody adviced this before. Hope this will help!

###

I have had issues trying to create directories on the SDCard as well. I found what was happening is that my code would work fine on some phones, and also the emulator. However other phones would report errors.

I believe there is an issue with certain phones, the Atrix and the Bionic are two examples.

These phones when using the Environment.getExternalStorageDirectory() code actually return /mnt/sdcard.

However on these phones the SD card is actually /mnt/sdcard-ext.

So somewhere along the lines it looks like some manufactures have broken the getExternalStorageDirectory code, and somehow its returning a false location.

I had a heck of a time figuring this out because my code worked on many different devices, and still some users reported it failed.

###

My problem was that I was using mkdir for creating a multiple directories path and I got false everytime. Once I used mkdirs my problem got resolved

###

I had the same problem. The permission was set and also the path was the default one (/mnt/sdcard) but my android was mounted as an external storage device for quick browsing the SD card.
However this interferes with other apps.

I figured this out when diving into the adb shell:

$ cd /mnt   
$ cd sdcard
cd: can't cd to sdcard
$ ls -l
d--------- system   system            2011-12-07 01:59 sdcard

after unmounting the image I’ve got:

$ ls -l                   
drwxrwxr-x system   sdcard_rw          2011-12-07 11:46 sdcard

As you can see while mounted the sdcard is in the system user group and there are no permissions at all. After unmounting the group is changed to sdcard_rw and permissions to read and write are granted.

###

I know this is an old posting but I thought I can still offer a suggestion. If you’re running your app while developing (ie. the phone is plugged into the PC via USB), then chances are, the SD card has been mounted by the PC. You have to unmount it (you can do it from the phone or from the PC) in order to gain write permission, this requires that has been set in the manifest.

###

I thought I was having this problem while debugging an app. It was failing because the SD Card was mounted by the host computer while debugging. Unmounting from the computer made the app work again.

###

It is possible that these users have an SD card that is corrupt and thus mounted read-only. If possible, you should check with them to see if anything else can write files to it. Given that you have the WRITE_EXTERNAL_STORAGE permission, you should have no trouble making modifications to the SD card (and the permissions of everything in the SD card is world read/write for such apps, so nothing can mess with file permissions to cause them trouble either).

###

I ran into this problem when the symlink /storage/emulated/0 was somehow missing. What I did was use the camera app to take a picture. I observed that the pictures details said it was within /storage/emulated/0, though that directory did not exist from the file explorer. Then I rebooted the phone. After reboot the directory showed up in file explorer.

###

I experienced a similar problem when using mkdirs(), however because running the command:

mkdir one/two

fails on Linux, then the API (http://download.oracle.com/javase/1.4.2/docs/api/java/io/File.html#mkdirs()) subsequently fails too. I guess this means there is no way to use mkdirs on Android? My (probably rather hacky) work-around was to create each necessary directory separately:

String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
new File(extStorageDirectory + "/one/").mkdirs();
new File(extStorageDirectory + "/one/two/).mkdirs();

###

I ran into this issue on virtual device. For me the issue was that I had defined no external storage for the virtual device. Hopefully helps someone.

###

I was having a similar problem. My camera app was working fine in all the android versions but Q (10).

The issue was that I using Environment.getExternalStorageDirectory(); and its deprecated in android Q (onwards). The solution was simple, using something that was supported in the latest version which will be context.getExternalFilesDir()

Here is how I get it working in all the devices:-

public static boolean createDirectory(Context context, String directoryPath) {
    File folder;

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
        folder = new File(context.getExternalFilesDir(null), directoryPath);
    else
        folder = new File(Environment.getExternalStorageState(), directoryPath);

    if (!folder.exists()) {
        return folder.mkdirs();
    }

    return true;
}

Leave a Reply

Your email address will not be published. Required fields are marked *