reactjs 无法打开移动的相机,而使用expo go应用程序在android?

3j86kqsm  于 2023-01-25  发布在  React
关注(0)|答案(1)|浏览(179)

无法打开移动的摄像头,而使用expo Go应用程序在Android?相机是不是在react原生expo应用程序在Android真实的设备中打开,Incase我点击按钮打开相机,它显示一些警告消息。但如果我建立APK,相机是打开的,工作正常

import {
  launchCameraAsync,
  useCameraPermissions,
  PermissionStatus,
} from 'expo-image-picker';
import { Alert, Button, View } from 'react-native';

const ImagePicker = () => {
  const [cameraPermissionInformation, requestPermission] =
    useCameraPermissions();

  const verifyPermission = async () => {
    console.log('PermissionStatus.DENIED', PermissionStatus.DENIED);
    if (cameraPermissionInformation.status === PermissionStatus.UNDETERMINED) {
      const responseStatus = await requestPermission();
      return responseStatus.granted;
    }
    if (cameraPermissionInformation.status === PermissionStatus.DENIED) {
      // Alert.alert(
      //   'Insufficient Camera Permission!',
      //   'This app need camera permission'
      // );
      const permissionResponse = await requestPermission();
      console.log(
        'permissionResponse.granted-----',
        permissionResponse.granted
      );
      return permissionResponse;
      // return false;
    }
    return true;
  };
  const imagePickerHandler = async () => {
    const hasPermission = await verifyPermission();
    console.log('hasPermission ', hasPermission);
    if (!hasPermission) {
      return;
    }
    const image = await launchCameraAsync({
      allowsEditing: true,
      aspect: [16, 9],
      quality: 0.5,
    });
    // console.log(image);
  };
  return (
    <View>
      <View></View>
      <Button title='take picture' onPress={imagePickerHandler} />
    </View>
  );
};

export default ImagePicker;

Warning Message

zrfyljdw

zrfyljdw1#

    • 溶液1**
  • 清除您的世博会项目的记忆。运行此
npx expo start --clear
    • 溶液2**
  • 简单,但在某些情况下(比如我自己)可能有意义。关闭你的应用程序并重新加载。
    • 解决方案3**
  • 我个人的意见是,您可能希望在imagePickerHandler语句中更改您的return语句。
  • 那就是试着写
return <View/>
  • 您还可以尝试实现useEffect,这会使事情变得容易得多。
  • 例如,让我给出一个模型代码;
useEffect(() => {
      (async() => {
          const cameraStatus = await Camera.requestCameraPermissionsAsync()
          setHasCameraPermission(cameraStatus.status === 'granted')})()},
          [])

      if (hasCameraPermission === null) {
          return <View/>
        }
      if (hasCameraPermission === false){
          return <Text>Enable access for Camera to proceed</Text>
        }

相关问题