GetExternalFilesDir (null) returns null for some users

So my problem is the following:

the following line of code throws a NullPointerException for some users of my application, but I don't know why

String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state))
    baseDir = getExternalFilesDir(null).getAbsolutePath();
else
    baseDir = getFilesDir().getAbsolutePath();

      

more specifically, this part

baseDir = getExternalFilesDir(null).getAbsolutePath();

      

This code is called in OnPostCreate () and I also have the required permissions if you wondered.

So getExternalFilesDir (null) returns null for whatever reason, even if set.

I also searched SO a bit and found a related post with the same question, but no real answer just a good one .

So is this just a problem on some custom phones or is the problem on android in general ending?

+3


source to share


1 answer


This is strange. Android documentation says that it only returns null if no external memory is installed. I think this is correct:



String state = Environment.getExternalStorageState();
if(Environment.MEDIA_MOUNTED.equals(state)) {
    File baseDirFile = getExternalFilesDir(null);
    if(baseDirFile == null) {
        baseDir = getFilesDir().getAbsolutePath();
    } else {
        baseDir = baseDirFile.getAbsolutePath();
    }
} else {
    baseDir = getFilesDir().getAbsolutePath();
}

      

+4


source







All Articles