Android READ_EXTERNAL_STORAGE permission not working

前端 未结 4 1619
北荒
北荒 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:39

    Simply replace with the below one and try,

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    0 讨论(0)
  • 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:

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

    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:

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

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

    <uses-permission 
         android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    0 讨论(0)
  • 2020-12-07 01:54

    While debugging on some devices you need to grant permission explicitly by going to your settings --> apps --> MY_APPLICATION --> Permissions and then grant required permissions.

    0 讨论(0)
  • 2020-12-07 01:54

    In addition to adding the below in AndroidManifest.xml

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

    we also need to request permission in the activity for the app to actually have access to external storage. https://developer.android.com/training/permissions/requesting.html

    0 讨论(0)
提交回复
热议问题