无法在Android中创建外部文件目录.WRITE_EXTERNAL_STORAGE存在

ces*_*sar 8 android android-sdcard

我试图用我的两个ApplicationContext和我通话Service的Context访问外部目录.不幸的是,它一直在返回null,LogCat报告它无法创建外部目录.我确信我有WRITE_STORAGE_PERMISSION现在,但它仍然无效.我的设备正在运行API 10(2.3.3)vanilla android.有任何想法吗?

这是我的清单:

<?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>
Run Code Online (Sandbox Code Playgroud)

这里是代码搞砸的地方:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)

代码是a的一种方法,Runnable由a调用Service.该构造函数Runnable以a Context为参数.这是Context代码使用的.代码在成功的Log调用时抛出异常,这意味着外部存储存在且可用.

更新的捏造修正:

干净安装不起作用.
恢复到API 9不起作用,尽管它之前工作.

ces*_*sar 10

固定,但我真的不知道为什么它现在有效.我重新启动设备,然后突然间,它会再次运行; Context#getExternalFilesDir ()停止了回来null.即使它现在有效(感谢和道具The IT Crowd的罗伊和莫斯),我应该向谷歌或其他什么报告?

  • 这有效,但有时在清除我的应用数据后回来. (3认同)

Tod*_*ton 6

这是一个旧线程,但我遇到了同样的问题,发现我感到惊讶的原因并不常见.当我的设备连接到计算机/ Eclipse时,我试图写入外部存储器...我收到以下错误,

06-25 16:16:09.565:W/ApplicationContext(4290):无法创建外部文件目录06-25 16:16:09.565:W/System.err(4290):java.io.FileNotFoundException:/full_archive.zip :open failed:EROFS(只读文件系统)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):引起:libcore.io.ErrnoException:open failed:EROFS(只读文件系统)06-25 16:16:09.573:W/System.err(4290) ):在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

这是来自开发者指南......

警告:如果用户在计算机上安装外部存储,则外部存储可能不可用...

简单地从我的电脑/ IDE中分离手机就解决了我的问题.