Android READ_EXTERNAL_STORAGE permission not working

前端 未结 4 1626
北荒
北荒 2020-12-07 01:13

I\'m trying to create a simple app that can get an image from the Gallery app and display it on an imageButton. I\'m testing with API 21 on a phone running Android 5.0.1. Un

4条回答
  •  甜味超标
    2020-12-07 01:51

    PROBLEM
    Your have the error

    java.lang.SecurityException: Permission Denial: reading com.android.providers.media.MediaProvider uri content://media/external/images/media/35634 from pid=25240, uid=10070 requires android.permission.READ_EXTERNAL_STORAGE, or grantUriPermission().

    CAUSE
    You are not giving correctly permissions because of android:maxSdkVersion="18", which according to documentation.

    The highest API level at which this permission should be granted to your app. Setting this attribute is useful if the permission your app requires is no longer needed beginning at a certain API level.

    As long as you didn't give permissions to API 21 your app is behaving ok.

    Check this topic for further info.

    SOLUTION
    If you wanna give read permissions under API 21, you must declare them as:

    
    

    IMPORTANT:

    This only make sense when you need to give extra permissions because some old api's require permissions that new versions doesn't, so user experience get's improved because you only ask for certain permissions when needed.


    So (in this case) the correct way will be:

    
    

    ADD ON
    If you want to save data you must add WRITE_EXTERNAL_STORAGE permissions.

    
    

提交回复
热议问题