如何解决SecurityException:API 23(Marshmallow)上的Permission Denial错误?

时间:2016-03-29 13:16:16

标签: java android

我已经得到了这个奇怪的强制关闭我的应用程序当Android 6.0上的某人尝试上传我的应用程序上的照片,而应用程序在Android Kitkat上正常运行,这是我的logcat

03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime: FATAL EXCEPTION: main
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime: Process: com.ummahtalk.network, PID: 14882
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:53790 flg=0x1 }} to activity {com.ummahtalk.network/com.ummahtalk.network.activities.posts.PublishActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=14882, uid=10166 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3743)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3786)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.app.ActivityThread.-wrap16(ActivityThread.java)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5466)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:  Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media from pid=14882, uid=10166 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.os.Parcel.readException(Parcel.java:1620)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:493)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.content.ContentResolver.query(ContentResolver.java:435)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at com.ummahtalk.network.helpers.FilePath.getDataColumn(FilePath.java:117)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at com.ummahtalk.network.helpers.FilePath.getPath(FilePath.java:82)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at com.ummahtalk.network.activities.posts.PublishActivity.setImageUriValue(PublishActivity.java:468)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at com.ummahtalk.network.activities.posts.PublishActivity.onActivityResult(PublishActivity.java:238)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.app.Activity.dispatchActivityResult(Activity.java:6456)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.app.ActivityThread.deliverResults(ActivityThread.java:3739)
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3786) 
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.app.ActivityThread.-wrap16(ActivityThread.java) 
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1412) 
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102) 
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:148) 
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5466) 
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method) 
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
03-29 18:36:33.627 14882-14882/com.ummahtalk.network E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

3 个答案:

答案 0 :(得分:2)

Android-M ie,API 23引入Runtime Permissions以减少Android设备中的安全漏洞,用户现在可以在运行时直接管理应用程序权限。如果用户拒绝您的应用程序的特定权限,则必须获取它通过询问您在查询中提到的权限对话框。

因此请在行动前检查,即检查您是否有权访问资源link,如果您的应用程序没有该特定权限,您可以请求权限link并处理权限请求回复如下。

@Override
public void onRequestPermissionsResult(int requestCode,
        String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_READ_CONTACTS: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted, yay! Do the
                // contacts-related task you need to do.

               } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
            return;
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

最后,如果您计划使用新版本以避免强行关闭,那么通过behavior changes是一个很好的做法:)

Permissions Best Practices.

您可以下载官方示例应用here

答案 1 :(得分:1)

您的应用程序看起来没有正确的权限(READ_EXTERNAL_STORAGE)。

问题是在Android 6中你必须在运行时检查权限。你可以在这里找到更多信息:
https://developer.android.com/training/permissions/requesting.html

答案 2 :(得分:1)

您需要在RunTime

请求权限

从API 23开始,您必须在运行时请求权限,因此不会自动授予Manifest的权限。

更多信息:http://developer.android.com/training/permissions/requesting.html

请记住,有时您必须向用户解释为什么您的应用程序需要这些权限。