Home > Software engineering >  React Native PermissionsAndroid doesn't include READ_MEDIA_IMAGES. How to work around this?
React Native PermissionsAndroid doesn't include READ_MEDIA_IMAGES. How to work around this?

Time:01-06

I'm using react-native-camera-roll/[email protected] in my React Native app. For Android 13, the READ_EXTERNAL_STORAGE permission has been replaced by READ_MEDIA_IMAGES. I use the following code to check if permission has been granted:

const permission = Platform.Version >= 33 ? PermissionsAndroid.PERMISSIONS.READ_MEDIA_IMAGES : PermissionsAndroid.PERMISSIONS.READ_EXTERNAL_STORAGE;

const hasPermission = await PermissionsAndroid.check(permission);

However, PermissionsAndroid.PERMISSIONS doesn't contain READ_MEDIA_IMAGES, so permission is undefined. Passing undefined to PermissionsAndroid.check() makes the app hang.

What I Want To Know:

I'm using [email protected]. PermissionsAndroid.PERMISSIONS.READ_MEDIA_IMAGES doesn't become available until 0.70, so unless I update to 0.70, permission will always be undefined. How can I work around this?

CodePudding user response:

One of the solutions is to use the separate well maintained library for permissions handling called https://github.com/zoontek/react-native-permissions

If you want to get it from RN it should be included in 0.71 based on this PR https://github.com/facebook/react-native/commit/0a854c7c8b7ffc382c43fa460651a4b4de34c3c7

Based on PR above you also can create a patch from your local changes using https://github.com/ds300/patch-package. However, if you want to use patched version of RN you should follow this instruction https://reactnative.dev/contributing/how-to-build-from-source

  • Related