gpt4 book ai didi

android - MediaStore.Images.Media.insertImage 在某些设备上抛出权限拒绝

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:46:33 26 4
gpt4 key购买 nike

我的代码行有问题:

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

Activity 不为空,位图不为空。

我得到错误:

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)

我的 list 中有权限:

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

适用于 5.1.1 Nexus 4。不适用于 6.0 Nexus 6。

你知道我该如何解决它吗?也许以编程方式添加一些权限?

最佳答案

引用 developers.android.com :

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 或更高时,您需要直接请求权限应用程序运行时的用户。

API 级别低于 23 的设备没有任何变化,这就是为什么您在使用 Nexus 4 时没有任何问题。

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

关于android - MediaStore.Images.Media.insertImage 在某些设备上抛出权限拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33277957/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com