How to Ask Running Permissions for Android in React Native

Last Updated on October 5, 2018.

Post Android Lollipop OS, you need to ask for running permissions to access camera, location, external memory etc. This React Native tutorial will help you how to ask running permissions in React Native.

Here, you should use PermissionsAndroid API from React Native. There are two types of permissions- normal and dangerous. Normal permissions are granted by default wherever dangerous permissions are granted by user when you make a prompt. In any case, you need to add those permissions in Android Manifest file.

In this tutorial, I want Write External Storage permission to be granted by user. First of all add the permission

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

in android/app/src/AndroidManifest.xml.

The code snippet given below will take care of the running permission for external memory.

async downloadFile() {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE,
        {
          title: "Storage Permission",
          message: "App needs access to memory to download the file "
        }
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        Alert.alert("Permission granted","Now you can download anything!");
      } else {
        Alert.alert(
          "Permission Denied!",
          "You need to give storage permission to download the file"
        );
      }
    } catch (err) {
      console.warn(err);
    }
  }

Need to know more about Android running permissions in react native? Go here.

Leave a Reply

%d bloggers like this: