Android Permission Error Even with declaration in manifest.xml

孤街醉人 提交于 2019-12-11 10:42:37

问题


I am trying to open an intent to choose a photo from the android gallery and then set an image view in the parent activity with the chosen image. I am able to open gallery and choose and image but then the parent activity crashes in the onActivityResult() method. The crash occurs in the Cursor instantiation line.

    <!--
         The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
         Google Maps Android API v2, but you must specify either coarse or fine
         location permissions for the 'MyLocation' functionality. 
    -->
    <!--<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />-->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <!-- To auto-complete the email text field in the login form with the user's emails -->
    <uses-permission android:name="android.permission.GET_ACCOUNTS" />
    <uses-permission android:name="android.permission.READ_PROFILE" />
    <uses-permission android:name="android.permission.READ_CONTACTS" />

Parent Activity on Result method

        Uri selectedImage = data.getData();
        if(!isExternalStorageReadable()) {
            Log.d("NAMSD", "SS");
        } else {
            Log.d("hey", selectedImage.toString());

            String[] filePathColumn = {MediaStore.Images.Media.DATA};


            Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);

            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

            String picturePath = cursor.getString(columnIndex);

            cursor.close();

            Bitmap imageBitmap = BitmapFactory.decodeFile(picturePath);

            photo.setImageBitmap(imageBitmap);
        }

Stach Trace:

02-16 16:09:54.108 1803-1815/? E/DatabaseUtils: Writing exception to parcel java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/37 from pid=8382, uid=10057 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.content.ContentProvider.enforceReadPermissionInner(ContentProvider.java:605) at android.content.ContentProvider$Transport.enforceReadPermission(ContentProvider.java:480) at android.content.ContentProvider$Transport.query(ContentProvider.java:211) at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112) at android.os.Binder.execTransact(Binder.java:453) 02-16 16:09:54.108 8382-8382/? D/AndroidRuntime: Shutting down VM 02-16 16:09:54.109 8382-8382/? E/AndroidRuntime: FATAL EXCEPTION: main Process: io.github.getExposure, PID: 8382 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { dat=content://media/external/images/media/37 }} to activity {io.github.getExposure/io.github.getExposure.PostViewActivity}: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/37 from pid=8382, uid=10057 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.app.ActivityThread.deliverResults(ActivityThread.java:3699) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) at android.app.ActivityThread.-wrap16(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/37 from pid=8382, uid=10057 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission() at android.os.Parcel.readException(Parcel.java:1599) 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:491) at android.content.ContentResolver.query(ContentResolver.java:434) at io.github.getExposure.PostViewActivity.onActivityResult(PostViewActivity.java:135) at android.app.Activity.dispatchActivityResult(Activity.java:6428) at android.app.ActivityThread.deliverResults(ActivityThread.java:3695) at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)  at android.app.ActivityThread.-wrap16(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 


回答1:


The question is not clear, but I suspect you are compiling with android-23 API (Marshmallow) and It has introduced the new way of asking the user for permission on the spot of using it, so have two choices

  1. (Recommended) Ask the user for that permission before using it. More info here.
  2. (NOT recommended) Change you target API to a pre-Marshmallow version and that reverts to the old way of asking about all the permissions on application installation.



回答2:


    permissions = new String[2];
    permissions[0] = Manifest.permission.WRITE_EXTERNAL_STORAGE;
    permissions[1] = Manifest.permission.READ_EXTERNAL_STORAGE;
    ActivityCompat.requestPermissions(
            this,
            permissions,
            5
    );

I just needed to explicitly ask for permission to read/write external storage in my activity because of android marshmallow.



来源:https://stackoverflow.com/questions/35445733/android-permission-error-even-with-declaration-in-manifest-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!