Dewald Swanepoel
Dewald Swanepoel

Reputation: 1681

getExternalFilesDir(null) returns null

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.

Upvotes: 23

Views: 39729

Answers (4)

sp00ky
sp00ky

Reputation: 161

My issue was that I opened a FileOutputStream, then before I closed the FileOutputStream, I opened a FileInputStream to see what was already in the file.

I moved opening the FileInputStream to before the FileOutputStream is opened and that fixed my issue.

Upvotes: 0

user3024255
user3024255

Reputation:

Delete a line

uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"   

in AndroidManifest.xml.

Switch a xml editor to "Android Manifest Permissions" with "Permission" tab in eclipse, and add a

uses-permission "android.permission.WRITE_EXTERNAL_STORAGE" 

with some clicks.

Then try running your application.


It seems eclipse (may depends on a defference of version or state or settings) can ignore some words described by direct xml in AndroidManifest.xml.


Thanks for an advise. You are right, my answer looked like to agree in small talk.

Upvotes: -1

Alex Rashkov
Alex Rashkov

Reputation: 10005

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

Upvotes: 23

FunkTheMonk
FunkTheMonk

Reputation: 10938

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" />

Upvotes: 35

Related Questions