Android External Storage Issues

I am trying to store data on an SD card, according to the guide here I have to use getExternalFilesDir() (API level 8), I would really like to do this, but by calling File directory = context.getExternalFilesDir(null); keeps returning null , I use my main action as a context.
Then I tried to revert to using File dir = Environment.getExternalStorageDirectory(); , it returned the SD root correctly, but apparently I was unable to create the proposed directory "/ Android / data / package_name / files /":

  File root = Environment.getExternalStorageDirectory(); File dir = new File(root, GlobalConstants.EXTERNAL_SAVE_DIR); boolean canCreate = dir.mkdirs(); 

In this code, canCreate is false (EXTERNAL_SAVE_DIR explicitly / Android / data / package _name / files /).

Is there anything that I am missing, maybe I should ask for permission to manifest (I did not find anything)? I read getExternalFilesDir() has an error in which the content is deleted when the application is updated, I do not care, and I would prefer to use it instead of another, as I try to match API 8, and I like the idea of ​​handling the directory name for me.

Any idea?

+6
android
source share
1 answer

You need to add the following to the manifest:

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

Otherwise, you are not allowed to write to the SD card. If you use Eclipse, you can use the Permissions tab of the manifest editor, add it.

+21
source share

All Articles