无法在 Android 中创建外部文件目录。 WRITE_EXTERNAL_STORAGE 存在
我尝试使用我的 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>
这里是代码混乱的地方:
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 不起作用,尽管它之前可以工作。
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
已修复,但我真的不知道为什么它现在有效。 我重新启动设备,然后突然间,它又可以工作了;
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 returningnull
. Even though it works now (thanks and props toThe IT Crowd
's Roy and Moss), should I report this to Google or something?这是一个旧线程,但我刚刚遇到了同样的问题,发现令我惊讶的原因并不常见。当我的设备连接到计算机/Eclipse 时,我试图写入外部存储。并且出现以下错误,
这是来自开发人员指南...
只需将手机与计算机/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,
This is from the Developer Guide...
Simply detaching the phone from my computer/IDE solved the problem for me.
您需要在 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.