0

On some customer devices calling getExternalFilesDir( null ) (and rarely getFilesDir()) from Application.onCreate() returns null.

The documentation says nothing about not being able to call these methods from an Application object and on most devices they work just fine. Anybody have any idea why these methods would return null and should I call them exclusively from an Activity object?

user656226
  • 101
  • 2
  • 4

3 Answers3

7

Add

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

to your Manifest. Why Write not Read (when only reading the directory)- I do not know. I suppose every application need it in order to create the directories when using the sdcard for the first time.

ironbone
  • 91
  • 1
  • 7
0

From the docs for ContextWrapper.getExternalFilesDir

Returns

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.

Are you checking to make sure the external storage is mounted before calling that method?

Squonk
  • 48,735
  • 19
  • 103
  • 135
0

Some of our clients are also getting this. Luckily, it suddenly reproduced on one of our test devices.
After some sniffing around I found that a weird file appeared under Android/data/ named like the app package name but with a size of 0. I couldn't delete or rename it. While this file existed, the app could not access external storage.
After some attempts, I rebooted the device and navigated to Android/data. I saw that the strange file disappeared and a folder of the same name appeared. The application started functioning normally.
I assume that this is a bug in Android application installer. I will start advising clients that report this issue to restart their devices.

Gili Garibi
  • 416
  • 2
  • 13