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

发布于 2024-12-07 08:04:15 字数 2959 浏览 3 评论 0 原文

我尝试使用我的 ApplicationContext 和调用 ServiceContext 来访问外部目录。不幸的是,它不断返回 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>

这里是代码混乱的地方:

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;
    }
}

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

尝试修复的更新:

全新安装不起作用。
恢复到 API 9 不起作用,尽管它之前可以工作。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

暮色兮凉城 2024-12-14 08:04:16

已修复,但我真的不知道为什么它现在有效。 我重新启动设备,然后突然间,它又可以工作了; Context#getExternalFilesDir () 停止返回 null。尽管它现在可以工作(感谢并支持 The IT Crowd 的 Roy 和 Moss),我是否应该向 Google 报告此问题?

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?

吻安 2024-12-14 08:04:16

这是一个旧线程,但我刚刚遇到了同样的问题,发现令我惊讶的原因并不常见。当我的设备连接到计算机/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:打开失败:EROFS(只读文件系统)
06-25 16:16:09.565:W / System.err(4290):在libcore.io.IoBridge.open(IoBridge.java:416)
06-25 16:16:09.565:W / System.err(4290):在java.io.FileOutputStream。(FileOutputStream.java:88)
06-25 16:16:09.565:W / System.err(4290):在java.io.FileOutputStream。(FileOutputStream.java:73)
06-25 16:16:09.565:W / System.err(4290):在com.seine.tropy.database.DownloaderThread.run(DownloaderThread.java:79)
06-25 16:16:09.573:W/System.err(4290):引起:libcore.io.ErrnoException:打开失败:EROFS(只读文件系统)
06-25 16:16:09.573:W / System.err(4290):在libcore.io.Posix.open(本机方法)
06-25 16:16:09.573:W / System.err(4290):在libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
06-25 16:16:09.573:W / System.err(4290):在libcore.io.IoBridge.open(IoBridge.java:400)
06-25 16:16:09.573: W/System.err(4290): ... 3 更多

这是来自开发人员指南...

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

只需将手机与计算机/IDE 分离即可解决我的问题。

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.

停顿的约定 2024-12-14 08:04:16

您需要在 AndroidManifest.xml 中拥有 WRITE_EXTERNAL_STORAGE 权限。至少,这就是为我解决的问题。

行之前添加行

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文