Reputation: 818
I am trying to debug a problem I am having in my app. The issue is that getExternalFiles(dir) returns null in my android app but I am not sure why. I have the necessary permissions. This is a bug which exists on multiple devices and versions of android (2.3.5 and up). Let me know if you need any more information.
The line of code which is the problem is:
path = context.getExternalFilesDir(null) + "/database/mydata.db";
or
path = context.getFilesDir().getAbsolutePath() + "/database/mydata.db";
Thank you.
Upvotes: 2
Views: 11313
Reputation: 1841
In my case the problem was in the context
context.getExternalFilesDir(null) + "/database/mydata.db";
I passed a global context, that obtain from a separate class _MyApplication
. But I forgot to add it in the manifest. so, the solution was In the manifest :
<application
android:name="._MyApplication"
android:allowBackup="true"
.......
Upvotes: 0
Reputation: 689
Could be permissions issue Try to add this as well:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Upvotes: 3
Reputation: 23279
context.getExternalFilesDir(null)
returning null
is a perfectly valid response.
From the docs:
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.
So it looks like the devices where it is failing have an external SD card that is currently not available (user has mounted it, removed it, etc.).
You will have to check for this case using something like Environment.getExternalStorageState()
and handle accordingly if it's not there.
Upvotes: 4
Reputation: 1820
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
Upvotes: 3