Home > Mobile >  Why does the react-native-permissions cannot request the permission in Android?
Why does the react-native-permissions cannot request the permission in Android?

Time:07-15

Information

I tried to create an app to request permission in the react native side. There is a button in a screen to be clicked to check and request permission.

Problem

react-native-permission link: https://www.npmjs.com/package/react-native-permissions

When I follow the instruction in the link and create a demo app, I cannot request the permission and the request result is blocked.

Screenshots

Step 1: The main screen of app with permission request button

Step 2: The pop-out window

I click button at main screen(step 1), the step 2 will show and disappear quickly and back to screen at step 1.

I am new to React-Native, I don't know how to solve the problem. It is appreciated that anyone can help me to solve the problem. Thank you very much.

Github Link

https://github.com/TrifaC/HradwarePermissionTestApp.git

CodePudding user response:

Try PermissionsAndroid from react-native package

import { PermissionsAndroid} from "react-native";

const requestCameraPermission = async () => {
  try {
    const granted = await PermissionsAndroid.request(
      PermissionsAndroid.PERMISSIONS.CAMERA,
      {
        title: "Cool Photo App Camera Permission",
        message:
          "Cool Photo App needs access to your camera "  
          "so you can take awesome pictures.",
        buttonNeutral: "Ask Me Later",
        buttonNegative: "Cancel",
        buttonPositive: "OK"
      }
    );
    if (granted === PermissionsAndroid.RESULTS.GRANTED) {
      console.log("You can use the camera");
    } else {
      console.log("Camera permission denied");
    }
  } catch (err) {
    console.warn(err);
  }
};

CodePudding user response:

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

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

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

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

Please Add in your AndroidManifest.xml

  • Related