Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/i

最近在做调用系统相册,选择多照片问题,遇到了一个错,发现用原有的设置权限方式是不行的,总结一下解决办法,报错如下:

  • Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/i_第1张图片
  • ComponentInfo{com.example.chenkui.testpic/com.example.chenkui.testpic.TestPicActivity}:
    java.lang.SecurityException: Permission Denial: reading
    com.android.providers.media.MediaProvider uri
    content://media/external/images/thumbnails from pid=13557, uid=10627
    requires android.permission.READ_EXTERNAL_STORAGE, or
    grantUriPermission()

05-26 00:33:58.200 13557-13557/com.example.chenkui.testpic E/AndroidRuntime: FATAL EXCEPTION: main
                                                                             Process: com.example.chenkui.testpic, PID: 13557
                                                                             java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chenkui.testpic/com.example.chenkui.testpic.TestPicActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/thumbnails from pid=13557, uid=10627 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2793)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864)
                                                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567)
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:105)
                                                                                 at android.os.Looper.loop(Looper.java:156)
                                                                                 at android.app.ActivityThread.main(ActivityThread.java:6524)
                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:831)
                                                                              Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/thumbnails from pid=13557, uid=10627 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission()
                                                                                 at android.os.Parcel.readException(Parcel.java:1686)
                                                                                 at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:183)
                                                                                 at android.database.DatabaseUtils.readExceptionFromParcel(DatabaseUtils.java:135)
                                                                                 at android.content.ContentProviderProxy.query(ContentProviderNative.java:421)
                                                                                 at android.content.ContentResolver.query(ContentResolver.java:541)
                                                                                 at android.content.ContentResolver.query(ContentResolver.java:476)
                                                                                 at com.example.chenkui.testpic.AlbumHelper.getThumbnail(AlbumHelper.java:65)
                                                                                 at com.example.chenkui.testpic.AlbumHelper.buildImagesBucketList(AlbumHelper.java:174)
                                                                                 at com.example.chenkui.testpic.AlbumHelper.getImagesBucketList(AlbumHelper.java:256)
                                                                                 at com.example.chenkui.testpic.TestPicActivity.initData(TestPicActivity.java:88)
                                                                                 at com.example.chenkui.testpic.TestPicActivity.onCreate(TestPicActivity.java:72)
                                                                                 at android.app.Activity.performCreate(Activity.java:6910)
                                                                                 at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
                                                                                 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2746)
                                                                                 at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2864) 
                                                                                 at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                                 at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1567) 
                                                                                 at android.os.Handler.dispatchMessage(Handler.java:105) 

解决这个问题的办法是:

AndroidManifest.xml

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

在onCreate()里面添加:

private static final int REQUEST_PERMISSION = 0;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_bucket);


        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {

            int hasWritePermission = checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            int hasReadPermission = checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE);

            List permissions = new ArrayList();
            if (hasWritePermission != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.WRITE_EXTERNAL_STORAGE);
            } else {
//              preferencesUtility.setString("storage", "true");
            }

            if (hasReadPermission != PackageManager.PERMISSION_GRANTED) {
                permissions.add(Manifest.permission.READ_EXTERNAL_STORAGE);

            } else {
//              preferencesUtility.setString("storage", "true");
            }

            if (!permissions.isEmpty()) {
//              requestPermissions(permissions.toArray(new String[permissions.size()]), REQUEST_CODE_SOME_FEATURES_PERMISSIONS);

                ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_PHONE_STATE},
                        REQUEST_PERMISSION);
            }
        }





    @Override
    public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
        switch (requestCode) {
            case REQUEST_PERMISSION: {
                for (int i = 0; i < permissions.length; i++) {
                    if (grantResults[i] == PackageManager.PERMISSION_GRANTED) {


                        System.out.println("Permissions --> " + "Permission Granted: " + permissions[i]);
                    } else if (grantResults[i] == PackageManager.PERMISSION_DENIED) {
                        System.out.println("Permissions --> " + "Permission Denied: " + permissions[i]);
                    }
                }
            }
            break;
            default: {
                super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }
    }

你可能感兴趣的:(Android)