cesar
cesar

Reputation: 9074

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.

Upvotes: 8

Views: 11196

Answers (3)

Todd Painton
Todd Painton

Reputation: 697

This is an old thread but I just ran into the same problem and found the reason which I'm surprised is not more common. I was trying to write to the external storage while my device was connected to computer/Eclipse.. and I was getting the following error,

06-25 16:16:09.565: W/ApplicationContext(4290): Unable to create external files directory 06-25 16:16:09.565: W/System.err(4290): java.io.FileNotFoundException: /full_archive.zip: open failed: EROFS (Read-only file system) 06-25 16:16:09.565: W/System.err(4290): at libcore.io.IoBridge.open(IoBridge.java:416) 06-25 16:16:09.565: W/System.err(4290): at java.io.FileOutputStream.(FileOutputStream.java:88) 06-25 16:16:09.565: W/System.err(4290): at java.io.FileOutputStream.(FileOutputStream.java:73) 06-25 16:16:09.565: W/System.err(4290): at com.seine.trophy.database.DownloaderThread.run(DownloaderThread.java:79) 06-25 16:16:09.573: W/System.err(4290): Caused by: libcore.io.ErrnoException: open failed: EROFS (Read-only file system) 06-25 16:16:09.573: W/System.err(4290): at libcore.io.Posix.open(Native Method) 06-25 16:16:09.573: W/System.err(4290): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110) 06-25 16:16:09.573: W/System.err(4290): at libcore.io.IoBridge.open(IoBridge.java:400) 06-25 16:16:09.573: W/System.err(4290): ... 3 more

This is from the Developer Guide...

Caution: External storage can become unavailable if the user mounts the external storage on a computer ...

Simply detaching the phone from my computer/IDE solved the problem for me.

Upvotes: 7

user348040
user348040

Reputation:

You need to have the WRITE_EXTERNAL_STORAGE permission in your AndroidManifest.xml. At least, thats what solved it for me.

Add the line <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> before your <application ... line.

Upvotes: 2

cesar
cesar

Reputation: 9074

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?

Upvotes: 10

Related Questions