Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🐛 InitializationException - Device reporting less cameras than anticipated #3179

Open
3 of 5 tasks
ncoen97 opened this issue Sep 9, 2024 · 16 comments
Open
3 of 5 tasks
Labels
🐛 bug Something isn't working

Comments

@ncoen97
Copy link

ncoen97 commented Sep 9, 2024

What's happening?

I am encountering a crash reported in Crashlytics, related to androidx.camera.core.InitializationException. The issue seems to occur when the app attempts to initialize the camera, and it reports fewer cameras than expected, resulting in a CameraUnavailableException. This crash happens on real devices, and it mentions retrying initialization. However, I cannot reproduce the issue locally on my test devices, so I’m unable to investigate further in a controlled environment.

Reproduceable Code

const QRCodeScanner: React.FC<QRCodeScannerProps> = ({
  onCodeScanned,
  loading,
  handlePermissionBlocked,
}) => {
  const device = useCameraDevice('back');
  const camera = useRef<Camera>(null);
  const isFocused = useIsFocused();
  const appState = useAppState();
  const [cameraReady, setCameraReady] = useState(false);
  const {onLayout, layout} = useLayout();
  const hasPermission = useCameraPermissions(handlePermissionBlocked);
  const codeScanner = useCodeScanner({
    codeTypes: ['qr'],
    onCodeScanned,
  });

  const isActive = isFocused && appState === 'active' && !!layout && !loading;
  const isScanning = isActive && cameraReady;

  const onError = (err: CameraRuntimeError | string) =>
    Analytics.logError(err, 'Camera error');

  return device && hasPermission ? (
    <View style={styles.container} onLayout={onLayout}>
      {isScanning ? (
        <QRMask
          containerHeight={layout.height}
          containerWidth={layout.width}
        />
      ) : null}
      {!isScanning ? (
        <View style={styles.loaderContainer}>
          <DefaultLoader />
        </View>
      ) : null}
      <Camera
        ref={camera}
        onError={onError}
        photo={false}
        device={device}
        codeScanner={codeScanner}
        isActive={isActive}
        style={StyleSheet.absoluteFill}
        onPreviewStarted={() => setCameraReady(true)}
        onStopped={() => setCameraReady(false)}
      />
    </View>
  ) : null;
};

Relevant log output

Crashlytics report:

Fatal Exception: androidx.camera.core.InitializationException
androidx.camera.core.CameraUnavailableException: Device reporting less cameras than anticipated. On real devices: Retrying initialization might resolve temporary camera errors. On emulators: Ensure virtual camera configuration matches supported camera features as reported by PackageManager#hasSystemFeature. Available cameras: 0

androidx.camera.core.CameraX.lambda$initAndRetryRecursively$2$androidx-camera-core-CameraX (CameraX.java:433)

Camera Device

I cannot provide the JSON for the camera device since the issue occurs in production and I cannot reproduce it locally.

Device

The crash affects multiple devices, with the following breakdown: Motorola (69%) Samsung (17%) Xiaomi (14%). Operating system versions: Android 11 (62%) Android 13 (21%) Android 12 (17%).

VisionCamera Version

4.5.2

Can you reproduce this issue in the VisionCamera Example app?

No, I cannot reproduce the issue in the Example app

Additional information

@ncoen97 ncoen97 added the 🐛 bug Something isn't working label Sep 9, 2024
Copy link

maintenance-hans bot commented Sep 9, 2024

Guten Tag, Hans here 🍻

Thanks for your detailed report! It sounds like a tricky issue, and I appreciate you trying to gather as much information as possible. However, we need more logs to help mrousavy effectively troubleshoot this problem. Please capture the logs using adb logcat when the crash occurs on the real devices, so we can get better insights into what's happening.

If you need help with gathering the logs, feel free to ask! The more data we have, ze easier it will be to find a solution.

Looking forward to your update!

Note: If you think I made a mistake, please ping @mrousavy to take a look.

@LukasMod
Copy link

Hey, I have same problem on production, multiple crashes in crashlytics (its small % of users, but enough to be one of main reason for our crashes currently). Maybe my information will help you somehow to debug this.

"react-native": "0.73.8",
"react-native-vision-camera": "4.5.0"

I add stacktarce from one of crashes.
stacktrace.txt

Name of error is androidx.camera.core.CameraX.lambda$initAndRetryRecursively$2$androidx-camera-core-CameraX (CameraX.java:369). Mostly (46%) its Xiaomi devices, different androids.

@nthegedus
Copy link

Hi, same problem here in production too.

"react-native": "0.74.1",
"react-native-vision-camera": "4.3.2",

In Crashlytics there's this error description:
Captura de Tela 2024-09-11 às 11 22 37

@mrousavy
Copy link
Owner

Well, Camera APIs on Android suck.

@efstathiosntonas
Copy link

efstathiosntonas commented Sep 16, 2024

Started seing this out of the sudden on Android 9 on 4.5.2, rn 0.75.3.

Can reproduce it locally on Pixel 4XL emulator running Android 9. @mrousavy do you need any logs or screw camera apis 😂?

@efstathiosntonas
Copy link

efstathiosntonas commented Sep 17, 2024

Some extra information around the bug, Simulator Pixel 4XL Android 9, same thing happens on real devices though.

  1. Open device camera app
  2. open your app
  3. try to pick a photo, throws: [session/recoverable-error: An unknown error occurred while creating the Camera Session, but the Camera can recover from it.] ERROR [device/camera-already-in-use: The given Camera Device is already in use!]
  4. Close your app
  5. try to open device camera app again: Android throws: Camera error Can't connect to camera so it seems it brakes Android system too.
  6. only way to recover device camera app is to cold boot the emulator

stack:

FATAL EXCEPTION: pool-26-thread-2 (Ask Gemini)
Process: com.xxxxx, PID: 10365
androidx.camera.core.InitializationException: androidx.camera.core.CameraUnavailableException: Device reporting less cameras than anticipated. On real devices: Retrying initialization might resolve temporary camera errors. On emulators: Ensure virtual camera configuration matches supported camera features as reported by PackageManager#hasSystemFeature. Available cameras: 0
 at androidx.camera.core.CameraX.lambda$initAndRetryRecursively$2$androidx-camera-core-CameraX(CameraX.java:433)
 at androidx.camera.core.CameraX$$ExternalSyntheticLambda2.run(D8$$SyntheticClass:0)
 at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
 at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
 at java.lang.Thread.run(Thread.java:764)
 Suppressed: kotlinx.coroutines.internal.DiagnosticCoroutineContextException: [StandaloneCoroutine{Cancelling}@81d901b, java.util.concurrent.ThreadPoolExecutor@660c5b8[Running, pool size = 2, active threads = 1, queued tasks = 0, completed tasks = 2]]
Caused by: androidx.camera.core.CameraUnavailableException: Device reporting less cameras than anticipated. On real devices: Retrying initialization might resolve temporary camera errors. On emulators: Ensure virtual camera configuration matches supported camera features as reported by PackageManager#hasSystemFeature. Available cameras: 0

edit: tried with 4.5.3 (with [email protected]) and it throws the same error.

@LuongTruong
Copy link

Hello guys, I just found a way to reproduce the issue.

  1. From Edit Android Emulator -> Show Advanced Settings -> Camera: Front -> None and Back -> None
  2. Run your app on that emulator and the issue will appear

Thanks guys for pointing nearly all the versions that have the issue. As a result I use react-native-vision-camera: 4.2.1.

Below is my final version in package.json

"dependencies": {
  "react": "18.2.0",
  "react-native": "0.73.9",
  "react-native-reanimated": "3.15.2",
  "react-native-vision-camera": "4.2.1",
},

Hope it can help.

@Rag0n
Copy link

Rag0n commented Sep 21, 2024

Same issue with react-native-vision-camera 4.5.2

@Nader-CS
Copy link

same issue

@mannoeu
Copy link

mannoeu commented Oct 2, 2024

Same here:

react-native: 0.73.4
react-native-vision-camera: ^4.5.2

image

androidx.camera.core.InitializationException: androidx.camera.core.CameraUnavailableException: Device reporting less cameras than anticipated. On real devices: Retrying initialization might resolve temporary camera errors. On emulators: Ensure virtual camera configuration matches supported camera features as reported by PackageManager#hasSystemFeature. Available cameras: 0
        at androidx.camera.core.CameraX.lambda$initAndRetryRecursively$2$androidx-camera-core-CameraX(CameraX.java:433)
        at androidx.camera.core.CameraX$$ExternalSyntheticLambda0.run(Unknown:12)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
        at java.lang.Thread.run(Thread.java:1012)

Caused by: androidx.camera.core.CameraUnavailableException: Device reporting less cameras than anticipated. On real devices: Retrying initialization might resolve temporary camera errors. On emulators: Ensure virtual camera configuration matches supported camera features as reported by PackageManager#hasSystemFeature. Available cameras: 0
        at androidx.camera.core.CameraX.lambda$initAndRetryRecursively$2$androidx-camera-core-CameraX(CameraX.java:433)
        at androidx.camera.core.CameraX$$ExternalSyntheticLambda0.run(Unknown:12)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:644)
        at java.lang.Thread.run(Thread.java:1012)

@ShchipskiiYura
Copy link

same problem

@imamrobani
Copy link

same issue

"react-native": "0.73.9",
"react-native-vision-camera": "^4.5.2"

@Nader-CS
Copy link

any help ?

@39otrebla
Copy link

39otrebla commented Oct 22, 2024

Same issue here.

@DarshanSavaliyaGitHub
Copy link

Same issue here...

@marcos-cunha-gringo
Copy link

We are having the same issue here:

  • react-native-vision-camera: 4.5.0;
  • react-native: 0.74.5;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐛 bug Something isn't working
Projects
None yet
Development

No branches or pull requests