Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getExternalFilesDir(null) returns null

Tags:

Once again, I've come up against a question which has been asked and answered before but in my implementation it is still not working.

I'm calling getExternalFilesDir(null) right at the very start of my main activity's onCreate method. It returns null every time, whether I run it in an AVD or on my phone (Samsung Galaxy Plus).

Yes, I have the <uses-permission android:name="android.permissions.WRITE_EXTERNAL_STORAGE" /> line in my AndroidManifest.xml and yes, I am checking the external storage state before I make the call and it is mounted.

Here are the first three lines inside my onCreate() method. Actually, it's just after the super.onCreate() and setContentView() calls.

String state = Environment.getExternalStorageState(); File extFiles = getExternalFilesDir(null); File locFiles = getFilesDir(); 

So, once these three lines have executed, these are the values for the variables:

state == "mounted" extFiles == null locFiles == "/data/data/com.mypackage.name/files" 

Would anyone have any ideas as to why this might be?

-----EDIT-----

So I've tried another approach; Rather than using getExternalFilesDir(null), I tried using File basePath = new File(Environment.getExternalStorageDirectory(), "myAppName");

This is not ideal and I know that the Android documentation says, and I agree with it, that you should rather use getExternalFilesDir(). Seeing as that's not working for me though I had to try something else. This time the function does return a valid File object so, after the above line, the path of basePath is /mnt/sdcard/myAppName. So far, so good. When I check with DDMS I can see that /mnt/sdcard exists but not /mnt/sdcard/myAppName. This is to be expected. So I call boolean result = basePath.mkdirs();

But this returns false and when I check on the file system I can confirm that the myAppName subfolder has not been created. When I create the folder manually through DDMS and put files in it, I can read those files from my application but I can't write anything in that folder.

Please help! I'm at my wit's end.

like image 314
Dewald Swanepoel Avatar asked Aug 21 '13 19:08

Dewald Swanepoel


2 Answers

If this wasn't a typo when you posted your question, you'll probably hate yourself for this:

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

should be

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

like image 154
FunkTheMonk Avatar answered Oct 20 '22 00:10

FunkTheMonk


This is from Android documentation:

Returns the path of the directory holding application files on external storage. Returns null if external storage is not currently mounted so it could not ensure the path exists; you will need to call this method again when it is available.

The other option is you can check if External storage is available:

String state = Environment.getExternalStorageState(); File filesDir;  // Make sure it's available if (Environment.MEDIA_MOUNTED.equals(state)) {     // We can read and write the media     filesDir = getExternalFilesDir(null); } else {     // Load another directory, probably local memory     filesDir = getFilesDir(); } 
like image 37
Alex Rashkov Avatar answered Oct 20 '22 00:10

Alex Rashkov