MediaStore.Images.Media.insertImage在某些设备上的抛出权限拒绝 [英] MediaStore.Images.Media.insertImage throwing permission denial on some devices

查看:157
本文介绍了MediaStore.Images.Media.insertImage在某些设备上的抛出权限拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对代码行有疑问

String path = MediaStore.Images.Media.insertImage(activity.getContentResolver(), bitmap, "feedback", null);

活动不为空,位图也不为空.

Activity is not null, bitmap is not null.

我收到错误消息:

0-22 11:23:59.644 29514-29527/? E/DatabaseUtils: Writing exception to parcel
10-22 11:23:59.644 29514-29527/? E/DatabaseUtils: java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=1380, uid=10136 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
10-22 11:23:59.644 29514-29527/? E/DatabaseUtils:     at android.content.ContentProvider.enforceWritePermissionInner(ContentProvider.java:679)
10-22 11:23:59.644 29514-29527/? E/DatabaseUtils:     at android.content.ContentProvider$Transport.enforceWritePermission(ContentProvider.java:494)
10-22 11:23:59.644 29514-29527/? E/DatabaseUtils:     at android.content.ContentProvider$Transport.insert(ContentProvider.java:258)
10-22 11:23:59.644 29514-29527/? E/DatabaseUtils:     at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:163)
10-22 11:23:59.644 29514-29527/? E/DatabaseUtils:     at android.os.Binder.execTransact(Binder.java:453)
10-22 11:23:59.645 1380-1467/? E/MediaStore: Failed to insert image
10-22 11:23:59.645 1380-1467/? E/MediaStore: java.lang.SecurityException: Permission Denial: writing com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=1380, uid=10136 requires android.permission.WRITE_EXTERNAL_STORAGE, or grantUriPermission()
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at android.os.Parcel.readException(Parcel.java:1599)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at android.content.ContentProviderProxy.insert(ContentProviderNative.java:476)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at android.content.ContentResolver.insert(ContentResolver.java:1231)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:962)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at com.azimo.sendmoney.a.utils.Screenshot$1.call(Screenshot.java:58)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at com.azimo.sendmoney.a.utils.Screenshot$1.call(Screenshot.java:51)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at rx.Observable.unsafeSubscribe(Observable.java:7531)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at rx.internal.operators.OperatorSubscribeOn$1$1.call(OperatorSubscribeOn.java:62)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:269)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
10-22 11:23:59.645 1380-1467/? E/MediaStore:     at java.lang.Thread.run(Thread.java:818)

我的清单中有权限

 <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

适用于5.1.1 Nexus4.不适用于6.0 Nexus6.

Works on 5.1.1 Nexus 4. Doesn't work on 6.0 Nexus 6.

您是否知道如何解决?也许以编程方式添加一些权限?

Do you have any idea how could I fix it? Maybe adding some permissions programatically?

推荐答案

引用 developers.android.com :

从Android 6.0(API级别23)开始,用户向以下用户授予权限 应用在运行时(而不是在安装应用时)运行.这 这种方法简化了应用程序的安装过程,因为用户没有 他们在安装或更新应用程序时需要授予权限.它也是 使用户可以更好地控制应用程序的功能;例如, 用户可以选择授予相机应用访问该相机的权限,但不能 到设备位置.用户可以随时撤消权限 时间,请转到应用的设置"屏幕.

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. It also gives the user more control over the app's functionality; for example, a user could choose to give a camera app access to the camera but not to the device location. The user can revoke the permissions at any time, by going to the app's Settings screen.

系统权限分为两类,普通权限和 危险:

System permissions are divided into two categories, normal and dangerous:

  • 普通权限不会直接危害用户的隐私.如果你的 应用程式在清单中列出一般权限,系统会授予 权限自动.

  • Normal permissions do not directly risk the user's privacy. If your app lists a normal permission in its manifest, the system grants the permission automatically.

危险的权限可以授予该应用程序 访问用户的机密数据.如果您的应用列出正常 清单中的权限,系统将授予该权限 自动地.如果列出危险许可,则用户必须 明确批准您的应用.

Dangerous permissions can give the app access to the user's confidential data. If your app lists a normal permission in its manifest, the system grants the permission automatically. If you list a dangerous permission, the user has to explicitly give approval to your app.

READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE属于危险"类别,因此,当targetSdkVersion为23或更高时,您需要在应用运行时直接向用户请求权限.

READ_EXTERNAL_STORAGE and WRITE_EXTERNAL_STORAGE are in the Dangerous category, for this reason when targetSdkVersion is 23 or higher you need to request the permission directly to the user when the app is running.

API级别低于23的设备没有任何变化,这就是为什么Nexus 4没有任何问题的原因.

Nothing has changed for device with API level lower than 23, this is why you don't have any issue with Nexus 4.

您可以在此处找到有关如何检查和请求权限的更多信息: https://developer.android.com/training/permissions/requesting.html

You can find more information on how to check and request a permission here: https://developer.android.com/training/permissions/requesting.html

这篇关于MediaStore.Images.Media.insertImage在某些设备上的抛出权限拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆