Can't create External Files Dir in Android. WRITE_EXTERNAL_STORAGE is present

cesar picture cesar · Sep 28, 2011 · Viewed 10.9k times · Source

I tried using both my ApplicationContext and my calling Service's Context to access the external directory. Unfortunately, it keeps returning null, and LogCat reports it was unable to create the external directory. I'm sure I have the WRITE_STORAGE_PERMISSION present, but it still won't work. My device is running API 10 (2.3.3) vanilla android. Any ideas?

Here's my manifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="droid.signboard" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="10" android:targetSdkVersion="10" />


<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />



<application android:icon="@drawable/icon" android:label="@string/app_name"
    android:name="SignboardApp">
    <receiver android:name=".ApplicationStarter">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED"></action>
            <action android:name="droid.signboard.LAUNCHER_START"></action>
        </intent-filter>
    </receiver>
    <activity android:label="@string/app_name"
        android:screenOrientation="landscape" android:launchMode="singleTop"
        android:name=".view.Signboard">
        <intent-filter>
            <action android:name="android.intent.action.MAIN"></action>
            <category android:name="android.intent.category.LAUNCHER"></category>
        </intent-filter>
    </activity>
    <service android:name=".controller.MasterControllerService">
        <intent-filter>
            <action
                android:name="droid.signboard.LAUNCH_SERVICE_FROM_ACTIVITY"></action>
        </intent-filter>
    </service>


</application>
</manifest>

and here's where the code messes up:

private boolean canWriteEx () {

    String state = Environment.getExternalStorageState ();

    if (state.equals (Environment.MEDIA_MOUNTED)) {
        Log.i (TAG, "Can write to external directory: "
                + context.getExternalFilesDir (null).getAbsolutePath ());
        return true;
    } else {
        Log.i (TAG, "Cannot write to external directory: "
                + context.getExternalFilesDir (null).getAbsolutePath ());
        return false;
    }
}

The code is a method of a Runnable, that is called by a Service. The constructor of the Runnable takes a Context as its parameter. That is the Context used by the code. The code throws an exception at the Log call that succeeds, implying that external storage is present and available.

UPDATES OF ATTEMPTED FIXES:

A clean install doesn't work.
Reverting down to API 9 doesn't work, though it worked earlier.

Answer

cesar picture cesar · Sep 28, 2011

FIXED, but I don't really know why it works now. I reboot the device, then all of a sudden, it would work again; Context#getExternalFilesDir () stopped returning null. Even though it works now (thanks and props to The IT Crowd's Roy and Moss), should I report this to Google or something?