Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 415
cesar Avatar asked Sep 28 '11 17:09

cesar


1 Answers

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?

like image 108
cesar Avatar answered Sep 17 '22 19:09

cesar