Android 6.0 访问图库时,报错 requires android.permission.READ_EXTERNAL_STORAGE异常

代码如下:

   /**
     * 打开图库
     * @param context
     * @param requestCode
     */
    public static void openGallery(Activity context, int requestCode) {
        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,"image/*");
        context.startActivityForResult(intent, requestCode);
    }

以上代码很简单,使用Intent开启图库,挑选图片,运行效果很不理想。

Android 7.0模拟器上,报错:

11-10 10:07:02.241 4449-4468/? E/DatabaseUtils: Writing exception to parcel
       java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider   uri content://media/external/file from pid=4337, uid=10049 requires android.permission.READ_EXTERNAL_STORAGE 
       , or grantUriPermission()

       at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:608)
        at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:483)
        at android.content.ContentProvider$Transport.query(ContentProvider.java:212)
        at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
        at android.os.Binder.execTransact(Binder.java:565)

在AndroidManifest.xml中添加权限:

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

其次,在android 6.0及其以上,需要动态赋予权限。

Android6.0动态权限

如何动态请求权限,请阅读 Android6.0权限处理篇Android EasyPermission权限库

注意点

若是程序中已经赋予写入权限,则不需要在写读取权限。写入权限已经包含了读取权限。

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
发布了123 篇原创文章 · 获赞 191 · 访问量 58万+
展开阅读全文

关于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官方博客

分享到微信朋友圈

×

扫一扫,手机浏览