2

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.

8
  • @Tarsem, I have a check for that but since this is proprietary code, I'm hesitant about posting too much. Commented Dec 12, 2013 at 15:45
  • @KenWolf, No. The exact error is IllegalStateException: File null/database/mydata.db Commented Dec 12, 2013 at 15:50
  • 1
    According to the docs this will return null if external storage is not mounted. Are you sure it is mounted? "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." Commented Dec 12, 2013 at 15:51
  • @KenWolf, the other possibility we have is context.getFilesDir().getAbsolutePath() + "/database/mydata.db" if the media is not mounted. those are the two possible locations and one of those methods is returning null as a parent directory Commented Dec 12, 2013 at 15:53
  • Your database is either in the internal or external storage - where have you put it? getFilesDir() is internal storage. getExternalFilesDir() is external storage. I assume you haven't put it in two different places? It's not going to appear in both magically... Commented Dec 12, 2013 at 16:00

4 Answers 4

4

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.

http://developer.android.com/reference/android/content/Context.html#getExternalFilesDir(java.lang.String)

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.

Sign up to request clarification or add additional context in comments.

Comments

3
 String state = Environment.getExternalStorageState();
        if (Environment.MEDIA_MOUNTED.equals(state) || Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
            return true; 
        } 
        return false;

Comments

3

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

1 Comment

android.permission.WRITE_EXTERNAL_STORAGE is ignored by API level 11
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.