[英]Crash due to requiring READ_EXTERNAL_STORAGE permission even though it's in the Manifest
I've got the task of updating an Android application that was previously built with the following settings: 我的任务是更新以前使用以下设置构建的Android应用程序:
compileSdkVersion 19
buildToolsVersion '19.0.1'
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
}
I downloaded the latest Android Studio and ended up changing these to the following to get the app to build: 我下载了最新的Android Studio,最终将其更改为以下内容,以构建应用程序:
compileSdkVersion 21
buildToolsVersion '21.1.2'
defaultConfig {
minSdkVersion 15
targetSdkVersion 19
}
The previously built app worked perfectly without any crash. 先前构建的应用程序完美运行,没有崩溃。 Unfortunately when I build it now the app crashes with the following exception:
不幸的是,当我现在构建它时,应用程序崩溃,但以下异常:
Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=2476, uid=10053 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
Even though the permission is clearly in the manifest: 即使清单中清楚地显示了许可:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.dbotha.app">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
The crash occurs at the point of loading a cursor for MediaStore.Images.Media.EXTERNAL_CONTENT_URI
: 崩溃发生在为
MediaStore.Images.Media.EXTERNAL_CONTENT_URI
加载游标时:
new CursorLoader(
this,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[]{
MediaStore.Images.Media._ID,
MediaStore.Images.Media.BUCKET_ID,
MediaStore.Images.Media.BUCKET_DISPLAY_NAME,
MediaStore.Images.Thumbnails._ID
},
null,
null,
null
);
I'm a little bit stumped as to what to do beyond ripping the whole component out and reimplementing from scratch -- which I'd prefer to avoid ;) 除了将整个组件拆下来并从头开始重新实现之外,我对如何处理感到有些困惑-我宁愿避免;)
It turns out that a project dependency had a maxSDKVersion
attribute in the libraries in AndroidManifest.xml
for these permissions: 事实证明,项目依赖项在
AndroidManifest.xml
的库中具有以下权限的maxSDKVersion
属性:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
During the manifest merge process that attribute would get included and break things on any device running above API version 18. 在清单合并过程中,该属性将被包含在内,并在API版本18以上运行的任何设备上破坏东西。
One approach to fixing this is to add the following applications manifest that will ensure the maxSdkVersion
attributes don't make it into the merged AndroidManifest.xml
: 解决此问题的一种方法是添加以下应用程序清单,以确保
maxSdkVersion
属性不会使其进入合并的AndroidManifest.xml
:
<manifest
xmlns:tools="http://schemas.android.com/tools"
...>
<uses-permission tools:node="replace" android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
<uses-permission tools:node="replace" android:name="android.permission.READ_EXTERNAL_STORAGE" android:maxSdkVersion="18" />
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.