【发布时间】:2016-02-10 17:49:00
【问题描述】:
我已根据本论坛其他地方的指南更新了我的应用,以使用 Android 6.0 权限模型访问外部 SD 卡。虽然从新安装应用程序时应用程序/权限工作正常,但如果我从以前的版本(依赖于 AndroidManifest.xml 中设置的权限)升级应用程序,应用程序将被拒绝访问 SD 卡 - 我需要卸载现有的版本并重新安装它以重新获得访问权限。
谁能解释为什么当我替换已安装的应用版本时,此应用更新会“破坏”外部 SD 卡权限?
我使用的代码(在我的 MainActivity 的 onCreate 方法中)是:
if (Build.VERSION.SDK_INT >= 23) {
// For Marshmallow devices check permissions for accessing External SD card
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
logger.info("Permission is granted");
return;
} else {
logger.info("Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return;
}
}
else { //permission is automatically granted on sdk<23 upon installation
logger.info("Permission is granted");
return;
}
}
编辑: 当应用程序从 Lollipop 设备上的先前版本升级时,此代码返回“InvalidSourceFolder”:
File f = new File("/storage/external_SD/DCIM/Camera");
File file[] = f.listFiles();
//If file is null then the source folder isn't valid
if (file == null) {
return "InvalidSourceFolder";
}
【问题讨论】:
-
我应该补充一点,这是指在运行 Android 5.* (Lollipop) 的手机上升级应用程序
-
“应用程序被拒绝访问 SD 卡”——首先,您在此处显示的代码与 removable storage 无关。它适用于external storage。第二,你为什么要检查
Build.VERSION.SDK_INT >= 23两次?第三,您没有向我们显示任何文件访问代码,也没有显示您在访问外部存储之前如何确认您拥有权限,因此我们不知道“被拒绝访问”是什么意思。 -
“SD 卡”是指外部 SD 卡。对 SDK 23 的仔细检查是一个错字 - 道歉。 “拒绝访问”是指下面的代码返回“InvalidSourceFolder”
-
“我的意思是外置SD卡”——
WRITE_EXTERNAL_STORAGE与“外置SD卡”无关,也就是removable storage。 -
您的评论似乎与此处给出的建议不一致:stackoverflow.com/questions/32629792/…
标签: android android-sdcard android-6.0-marshmallow android-permissions