Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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

like image 656
Danny Avatar asked Oct 31 '10 08:10

Danny


2 Answers

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.

like image 58
Vladimir Ivanov Avatar answered Oct 09 '22 05:10

Vladimir Ivanov


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!

like image 40
Andrey_yog Avatar answered Oct 09 '22 05:10

Andrey_yog