Android: requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

1.最近在SDK6.0开发项目组遇到一个这样的问题:requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()

2.原因:从6.0(api 23)开始, android采用了Runtime permission request, 即在运行时请求权限,而不是在编译的时候,即使在manifest.xml里面添加了权限也要在运行时请求。

   但不是所有的permission都要Runtime request,涉及到资源文件请求时就会用到Runtime request,比如获取位置信息, content provider获取media的情况。

3.解决方法:

a) 首先确认在manefist里面添加了READ_EXTERNAL_STORAGE权限。

        b) 在代码中实现Runtime request:

        

@SuppressLint("NewApi")
private void requestReadExternalPermission() {
    if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
            != PackageManager.PERMISSION_GRANTED) {
        Log.d(TAG, "READ permission IS NOT granted...");
        
        if (shouldShowRequestPermissionRationale(Manifest.permission.READ_EXTERNAL_STORAGE)) {
            
            Log.d(TAG, "11111111111111");
        } else {
            // 0 是自己定义的请求coude
            requestPermissions(new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 0);
            Log.d(TAG, "222222222222");
        }
    } else {
        Log.d(TAG, "READ permission is granted...");
    }
}


    c)处理请求:
    
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    Log.d(TAG, "requestCode=" + requestCode + "; --->" + permissions.toString()
            + "; grantResult=" + grantResults.toString());
    switch (requestCode) {
        case 0: {
            
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

                // permission was granted
                // request successfully, handle you transactions

            } else {

                // permission denied
                // request failed
            }
            
            return;
        }
        default:
            break;

    }
}

发布了37 篇原创文章 · 获赞 13 · 访问量 5万+
展开阅读全文

关于Android的错误:Permission Denial: 。。。.READ_EXTERNAL_STORAGE, or grantUriPermission(

10-11

这是错误代码 ,在虚拟机运行时崩溃, E/AndroidRuntime: FATAL EXCEPTION: main Process: com.add.mp3list, PID: 7193 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.add.mp3list/com.add.mp3list.MainActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=7193, uid=10090 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) at android.app.ActivityThread.-wrap11(Unknown Source:0) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) at android.os.Handler.dispatchMessage(Handler.java:105) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6541) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/audio/media from pid=7193, uid=10090 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.os.Parcel.readException(Parcel.java:1942) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183) at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135) at android.content.ContentProviderProxy.query(ContentProviderNative.java:418) at android.content.ContentResolver.query(ContentResolver.java:754) at android.content.ContentResolver.query(ContentResolver.java:704) at android.content.ContentResolver.query(ContentResolver.java:662) at com.add.mp3list.MusicList.getMusicData(MusicList.java:26) at com.add.mp3list.MainActivity.onCreate(MainActivity.java:21) at android.app.Activity.performCreate(Activity.java:6975) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)  at android.app.ActivityThread.-wrap11(Unknown Source:0)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)  at android.os.Handler.dispatchMessage(Handler.java:105)  at android.os.Looper.loop(Looper.java:164)  at android.app.ActivityThread.main(ActivityThread.java:6541)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)  Application terminated. 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览