mkdirs returns false for directory on sd card while the parent directory is writable

早过忘川 提交于 2019-12-18 04:31:52

问题


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


回答1:


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.




回答2:


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




回答3:


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!




回答4:


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.




回答5:


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




回答6:


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.




回答7:


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.




回答8:


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.




回答9:


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




回答10:


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.




回答11:


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();



回答12:


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.



来源:https://stackoverflow.com/questions/4062357/mkdirs-returns-false-for-directory-on-sd-card-while-the-parent-directory-is-writ

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!