无法在Android中创建外部文件目录。 WRITE_EXTERNAL_STORAGE存在
我尝试使用我的ApplicationContext
和我的电话Service
的Context
来访问外部目录。不幸的是,它不断返回null
,LogCat报告它无法创建外部目录。我确信我有WRITE_STORAGE_PERMISSION
礼物,但它仍然不起作用。我的设备正在运行API 10(2.3.3)vanilla android。有任何想法吗?无法在Android中创建外部文件目录。 WRITE_EXTERNAL_STORAGE存在
这里是我的清单:
<?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
。该代码在日志调用中抛出异常,该日志调用成功,意味着外部存储器存在且可用。
最新通报尝试的修复:
一个干净的安装不起作用。
恢复到API 9不起作用,尽管它早期工作。
FIXED,但我真的不知道它为什么现在起作用。 我重新启动设备,然后突然间,它会再次工作; Context#getExternalFilesDir()
停止返回null
。尽管它现在可以工作(感谢和支持The IT Crowd
的Roy和Moss),我应该向Google报告还是其他?
这有效,但在清除我的应用程序的数据后有时会回来。 – offex
您需要在AndroidManifest.xml中拥有WRITE_EXTERNAL_STORAGE权限。至少,那是什么解决了我。
在您的<application ...
行之前添加行<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
。
我很惊讶有3个不同的根本原因。 – likejiujitsu
他已拥有此权限集。 – Vincent
这是最奇怪的事情,我移动应用程序行的权限_before_并且它现在可以工作。没有重新启动,但已经做了很多次。谢谢。 – John
这是一条旧线程,但我遇到了同样的问题,发现我很惊讶的原因并不多见。当我的设备连接到计算机/ 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):at libcore.io.IoBridge.open(IoBridge.java:416) 06-25 16:16:09.565:W/System.err(4290): (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):at com.seine.trophy.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):at libcore.io.IoBridge.open(IoBridge.java:400) 06-25 16:16:09.573:W/System.err的(4290):... 3个
这是从开发者指南...
注意:如果用户安装在计算机上的外部存储外部存储设备变得不可用...
简单地从我的电脑/ IDE分离手机解决了我的问题。
这也是我提出的答案。我不能相信这不是一个更常见的问题,也不是在大胆的地方更容易接近的地方。 – Gowiem
@ToddPainton我没有得到这个问题 - 也许你没有在设置中设置开发人员选项?我使用android 4.04 API 15. samsung S2。 – likejiujitsu
哦,没有设置。我的解决方案呢?统一。我无法忍受原生的android编程。并作为一个附加?我可以将相同的代码编译到Apple。 –
发布您的androidmanifest.xml文件。 – Otra
发布您的代码和任何相关的堆栈跟踪。没有它,我们所能做的就是在黑暗中猜测镜头。 – FoamyGuy
你想做什么?你为什么调用context.getExternalFilesDir(null)?告诉我们的目的,我们可能会更好地瞄准你。而且,你的logcat中的堆栈跟踪会很有帮助。 – IncrediApp