mrousavy / react-native-vision-camera

📸 A powerful, high-performance React Native Camera library.
https://react-native-vision-camera.com
MIT License
7.26k stars 1.06k forks source link

🐛 intermittent Crash Due to Permission #3070

Closed rsainiWin closed 1 month ago

rsainiWin commented 1 month ago

What's happening?

I want to take a picture. Sometime very rare but when a user takes a picture this leads to a crash going via the crash reporting tool. It says it happened due to NullPointerException: Attempt to invoke virtual method 'void android.view.View.dispatchCancelPendingInputEvents()' on a null object reference Backtracking further it takes me to the Camera Module

Reproduceable Code

// hooks
  const {hasPermission: hasCameraPermission, requestPermission} =
    useCameraPermission();
  const handlePermission = async () => {
    await requestPermission();

    const audioPermission = Camera.getMicrophonePermissionStatus();
    if (audioPermission !== 'granted') {
      await Camera.requestMicrophonePermission();
    }
  };
  useEffect(() => {
    handlePermission();
    navigation.setOptions({headerShown: false});
  }, [navigation]);

return (
{hasCameraPermission && device != null && isEnabled && (
                <PinchGestureHandler
                  onGestureEvent={onPinchGesture}
                  enabled={isActive}>
                  <Reanimated.View
                    onTouchEnd={onFocusTap}
                    style={StyleSheet.absoluteFill}>
                    <TapGestureHandler onEnded={onDoubleTap} numberOfTaps={2}>
                      <ReanimatedCamera
                        style={StyleSheet.absoluteFill}
                        device={device}
                        isActive={isEnabled}
                        ref={camera}
                        onInitialized={onInitialized}
                        onError={onError}
                        enableHighQualityPhotos={false}
                        onStarted={() => 'Camera started!'}
                        onStopped={() => 'Camera stopped!'}
                        format={format}
                        photoQualityBalance={'balanced'}
                        // fps={fps}
                        // photoHdr={format?.supportsPhotoHdr && enableHdr}
                        // videoHdr={format?.supportsVideoHdr && enableHdr}
                        lowLightBoost={
                          device.supportsLowLightBoost && enableNightMode
                        }
                        enableZoomGesture={false}
                        animatedProps={cameraAnimatedProps}
                        exposure={0}
                        // enableFpsGraph={true}
                        orientation="portrait"
                        photo={true}
                        video={false}
                        audio={hasMicrophonePermission}
                      />
                    </TapGestureHandler>
                  </Reanimated.View>
                </PinchGestureHandler>
              )}
              {!hasCameraPermission && (
                <View
                  style={[
                    StyleSheet.absoluteFill,
                    {
                      flexDirection: 'column',
                      alignItems: 'center',
                      justifyContent: 'center',
                      backgroundColor: themeGradient.lightColors?.grey0,
                      paddingHorizontal: 24,
                    },
                  ]}>
                  <View style={{gap: 24}}>
                    <Icon
                      name="camera-off"
                      type="feather"
                      size={isTablet() ? 128 : 72}
                      style={{opacity: 0.6}}
                      color={'white'}
                    />
                    <Text
                      style={[
                        TextFontStyles.textBody,
                        TextColorStyles.white,
                        TextAlignmentStyles.center,
                      ]}>
                      You've not given access to the camera, click on the button
                      below to give access.
                    </Text>
                    <WSGredientButton
                      onPress={async () => await Linking.openSettings()}
                      icon={{name: 'camera', size: 24, color: 'white'}}
                      size="lg"
                      title="Allow Camera Access"
                    />
                  </View>
                </View>
              )})

Relevant log output

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.dispatchCancelPendingInputEvents()' on a null object reference
     at android.view.ViewGroup.dispatchCancelPendingInputEvents(ViewGroup.java:4948)
     at android.view.ViewGroup.dispatchCancelPendingInputEvents(ViewGroup.java:4948)
     at android.view.ViewGroup.dispatchCancelPendingInputEvents(ViewGroup.java:4948)
     at android.view.ViewGroup.dispatchCancelPendingInputEvents(ViewGroup.java:4948)
     at android.view.ViewGroup.dispatchCancelPendingInputEvents(ViewGroup.java:4948)
     at android.view.ViewGroup.dispatchCancelPendingInputEvents(ViewGroup.java:4948)
     at android.view.ViewGroup.dispatchCancelPendingInputEvents(ViewGroup.java:4948)
     at android.view.ViewGroup.dispatchCancelPendingInputEvents(ViewGroup.java:4948)
     at android.view.ViewGroup.dispatchCancelPendingInputEvents(ViewGroup.java:4948)
     at android.view.ViewGroup.dispatchCancelPendingInputEvents(ViewGroup.java:4948)
     at android.view.View.cancelPendingInputEvents(View.java:23371)
     at android.app.Activity.cancelInputsAndStartExitTransition(Activity.java:5800)
     at android.app.Activity.startActivityForResult(Activity.java:6600)
     at android.app.Activity.requestPermissions(Activity.java:5665)
     at com.facebook.react.ReactActivityDelegate.requestPermissions(ReactActivityDelegate.java:191)
     at com.facebook.react.ReactActivity.requestPermissions(ReactActivity.java:110)
     at com.mrousavy.camera.CameraViewModule.requestPermission(CameraViewModule.kt:226)
     at com.mrousavy.camera.CameraViewModule.requestCameraPermission(CameraViewModule.kt:234)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.facebook.react.bridge.JavaMethodWrapper.invoke(JavaMethodWrapper.java:372)
     at com.facebook.react.bridge.JavaModuleWrapper.invoke(JavaModuleWrapper.java:188)
     at com.facebook.jni.NativeRunnable.run(Native Method)
     at android.os.Handler.handleCallback(Handler.java:958)
     at android.os.Handler.dispatchMessage(Handler.java:99)
     at com.facebook.react.bridge.queue.MessageQueueThreadHandler.dispatchMessage(MessageQueueThreadHandler.java:27)
     at android.os.Looper.loopOnce(Looper.java:230)
     at android.os.Looper.loop(Looper.java:319)
     at com.facebook.react.bridge.queue.MessageQueueThreadImpl$4.run(MessageQueueThreadImpl.java:228)
     at java.lang.Thread.run(Thread.java:1012)

Camera Device

{
  "formats": [],
  "sensorOrientation": "landscape-left",
  "hardwareLevel": "limited",
  "maxZoom": 1,
  "minZoom": 1,
  "maxExposure": 6,
  "supportsLowLightBoost": false,
  "neutralZoom": 1,
  "physicalDevices": [
    "ultra-wide-angle-camera"
  ],
  "supportsFocus": false,
  "supportsRawCapture": false,
  "isMultiCam": false,
  "minFocusDistance": 0,
  "minExposure": -6,
  "name": "10 (BACK) androidx.camera.camera2",
  "hasFlash": false,
  "hasTorch": false,
  "position": "back",
  "id": "10"
}

Device

Samsung Galaxy S23 Ultra,,Google Pixel 7 , Samsung Galaxy S21 FE 5G , Samsung Galaxy S22,Samsung Galaxy S23 , Samsung Galaxy S24 , Samsung Galaxy Tab S6 Lite , Samsung Galaxy Tab S7+ 5G (SM-T978U)

VisionCamera Version

4.0.0-beta.13

Can you reproduce this issue in the VisionCamera Example app?

Intermittent issue unable to replicate

Additional information

maintenance-hans[bot] commented 1 month ago

Guten Tag, Hans here.

[!NOTE] New features, bugfixes, updates and other improvements are all handled mostly by @mrousavy in his free time. To support @mrousavy, please consider 💖 sponsoring him on GitHub 💖. Sponsored issues will be prioritized.

mrousavy commented 1 month ago

Any reason you are on 4 beta? Why not the latest stable release?

rsainiWin commented 1 month ago

It was the latest at the point of time And current latest build requires us to update react native to 0.74 We've updated to the latest build but this is pipeline

mrousavy commented 1 month ago

Not sure if I understood that, but can you please try to upgrade to VisionCamera 4.4.2 and try if this problem still exists?

rsainiWin commented 1 month ago

I've tried with latest version as well The crash happened randomly after giving the permission . I wasn't able to replicate it next time

Aditionally I was able to click the picture without any issues however after like 2-3 mintues of using the app it crashed FYI I was not on the Camera screen when it crashed

mrousavy commented 1 month ago

To be honest this doesn't sound like a VisionCamera issue - maybe you are rolling a custom RN fork or an old RN version, but I have never seen this happen before.

rsainiWin commented 1 month ago

I'm using the latest RN and I'm not using a custom fork Let me know if i can help any further

srgoogle23 commented 1 week ago

@mrousavy and @rsainiWin finded a issue on rn repo, https://github.com/facebook/react-native/issues/41077 and it's about react-native-vision-camera and the same problem