Home > Software engineering >  Photo still not seen after PermissionsAndroid.PERMISSIONS.CAMERA is granted
Photo still not seen after PermissionsAndroid.PERMISSIONS.CAMERA is granted

Time:01-22

My React Native 0.66 app needs to have CAMERA permission to access the photos in gallery in Android. Following is the permission function added to splash screen when the app is being launched.

//require Camera permission when open gallery
  const requestCameraPermission = async () => {
    try {
      const granted = await PermissionsAndroid.request(
        PermissionsAndroid.PERMISSIONS.CAMERA,
        {
          title: "添加权限",
          message:
            "app正常运行需要用到相册。请点OK ",
          buttonNegative: "Cancel",
          buttonPositive: "OK"
        }
      );
      if (granted === PermissionsAndroid.RESULTS.GRANTED) {
        return true; //console.log("You can use the camera");
      } else {
        console.log
      }
    } catch (err) {
     
      console.log(err);
    }
  };

if (Platform.OS === 'android') requestCameraPermission();

When the gallery is accessed first time, the function above pops up a window asking for the permission. After click OK, however, the image picked is still not seen in the app. Also in device under Setting -> App -> the app permission, permission for CAMERA was not there. BTW, the config (which is not shown here) for image picking on iOS works fine.

CodePudding user response:

You need to add permission in your Native Modules.

In AndroidManifest.xml(Android),

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

In Info.plist(iOS),

<key>NSCameraUsageDescription</key>
    <string>Your own description of the purpose</string>

<key>NSPhotoLibraryAddUsageDescription</key>
    <string>Your message to user when the photo library is accessed for the first time</string>

<key>NSPhotoLibraryUsageDescription</key>
<string>Your message to user when the photo library is accessed for the first time</string>

Also in you app/build.gradle file,

 defaultConfig {
    ...
    missingDimensionStrategy 'react-native-camera', 'general' // <--- insert this line
  }
  •  Tags:  
  • Related