Android external data storage problems
I'm trying to store data on the SD card, according to the guide here I should be using getExternalFilesDir()
(API level 8), I would really like to do so but calling
File directory = context.getExternalFilesDir(null);
keeps returning null
, I'm using my main activity as context.
Then I tried reverting to use File dir = Environment.getExternalStorageDirectory();
, it correctly returns the SD root, but apparently I am unable to create the suggested "/Android/data/package_name/files/" directory:
File root = Environment.getExternalStorageDirectory();
File dir = new File(root, GlobalConstants.EXTERNAL_SAVE_DIR);
boolean canCreate = di开发者_如何转开发r.mkdirs();
In this code canCreate
is false (EXTERNAL_SAVE_DIR is obviously /Android/data/package_name/files/).
Is there anything I'm missing, maybe should I ask permissions on the Manifest (I found nothing about)? I've read getExternalFilesDir()
has a bug where contents get deleted on application update, I don't really care about that and I would rather use it instead of the other one as I'm trying to conform to API 8 and I like the idea of it handling the directory name for me.
Any idea?
You need to add the following to your 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 are using Eclipse, you can use the permissions tab of the manifest editor do add it.
精彩评论