如何检查用户是否在React Native Android中授予了相机权限?

时间:2018-06-07 06:44:32

标签: android react-native android-camera react-native-android react-native-camera

应用启动后,我即可获得用户使用相机的权限。但是在打开相机之前,如果我检查用户是否已授予相机许可,即使允许,我也会将响应视为“假”。

这是我的代码:

try {
    Stream<Path> paths = Files.walk(Paths.get(location)); // Exception line

    List<Path> pathList = paths.filter(Files::isRegularFile).collect(Collectors.toList());
    for (Path path : pathList) {
        File file = path.toFile();

        // Business Code
        // Scope......
    }
} catch (Exception e){
    // java.nio.file.NoSuchFileException: E:\MisJob\CMIS\MONITOR_April_2018_error
}

2 个答案:

答案 0 :(得分:4)

请尝试以下方法: PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA).then(response => { ..,而非PermissionsAndroid.check('camera').then(response => { ..

答案 1 :(得分:0)

我在这里与 async await promise 一起使用,更好理解。

import {PermissionsAndroid} from 'react-native';

export const checkReadContactsPermission = async ()=>{    

  //result would be false if not granted and true if required permission is granted.
  const result =  await PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.CAMERA);
  return result;
}