Heim > Fragen und Antworten > Hauptteil
Android4.4手机上,在外置SD卡创建目录时,报错如下:
11-30 12:49:39.685 32097-32139/com.test.appstore W/System.err: java.io.IOException: open failed: EACCES (Permission denied)
11-30 12:49:39.685 32097-32139/com.test.appstore W/System.err: at java.io.File.createNewFile(File.java:950)
11-30 12:49:39.685 32097-32139/com.test.appstore W/System.err: at com.thin.downloadmanager.DownloadDispatcher.transferData(DownloadDispatcher.java:241)
11-30 12:49:39.685 32097-32139/com.test.appstore W/System.err: at com.thin.downloadmanager.DownloadDispatcher.executeDownload(DownloadDispatcher.java:157)
11-30 12:49:39.685 32097-32139/com.test.appstore W/System.err: at com.thin.downloadmanager.DownloadDispatcher.executeDownload(DownloadDispatcher.java:171)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err: at com.thin.downloadmanager.DownloadDispatcher.executeDownload(DownloadDispatcher.java:171)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err: at com.thin.downloadmanager.DownloadDispatcher.executeDownload(DownloadDispatcher.java:171)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err: at com.thin.downloadmanager.DownloadDispatcher.run(DownloadDispatcher.java:99)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err: Caused by: libcore.io.ErrnoException: open failed: EACCES (Permission denied)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err: at libcore.io.Posix.open(Native Method)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:110)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err: at java.io.File.createNewFile(File.java:943)
11-30 12:49:39.695 32097-32139/com.test.appstore W/System.err: ... 6 more
说明一下我这边的代码。
AndroidManifest中已经声明了读写SD卡的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
同时,在使用外置SD卡时,已经判断了外置sd卡的加载状态:
private static boolean isSdAvailable() {
return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
}
private static String getDir(Context context, String cache) {
StringBuilder path = new StringBuilder();
if (isSdAvailable()) {
// 如果内存卡可以,则存储在内存卡上
path.append(Environment.getExternalStorageDirectory().getAbsolutePath());
path.append(File.separator);
path.append(ROOT);
path.append(File.separator);
path.append(cache);
path.append(File.separator);
} else {
// 存放在手机内存中
File fileDir = context.getCacheDir();
path.append(fileDir.getAbsolutePath());
path.append(File.separator);
path.append(cache);
path.append(File.separator);
}
return path.toString();
}
求指导一下原因。
补充:我这边找到了出问题的手机,从手机状态来看,是手机的SD卡权限设置错误导致的。代码本身已经是没有问题,故关闭该问题。
伊谢尔伦2017-04-17 17:57:41
Android4.4版本后 限制第三方应用读写外置SD卡的权限;
如果要在外置sd卡写文件路径要在"Android/data/应用包名"下面
PHP中文网2017-04-17 17:57:41
同样的代码我在虚拟机遇到过这个问题,换到真机后没有这个问题,加下这个试试看,休眠唤醒的;
<uses-permission android:name="android.permission.WAKE_LOCK" />