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?

3 Answers 3

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.

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

Comments

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?

1 Comment

Yes I am with Environment.getExternalStorageState()
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.

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.