react-native-vision-camera icon indicating copy to clipboard operation
react-native-vision-camera copied to clipboard

šŸ› InitializationException - Device reporting less cameras than anticipated

Open ncoen97 opened this issue 1 year ago • 13 comments

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 avatar Sep 09 '24 18:09 ncoen97

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.

maintenance-hans[bot] avatar Sep 09 '24 18:09 maintenance-hans[bot]

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.

LukasMod avatar Sep 11 '24 12:09 LukasMod

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

nthegedus avatar Sep 11 '24 14:09 nthegedus

Well, Camera APIs on Android suck.

mrousavy avatar Sep 12 '24 11:09 mrousavy

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 avatar Sep 16 '24 20:09 efstathiosntonas

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.

efstathiosntonas avatar Sep 17 '24 06:09 efstathiosntonas

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.

LuongTruong avatar Sep 19 '24 08:09 LuongTruong

Same issue with react-native-vision-camera 4.5.2

Rag0n avatar Sep 21 '24 08:09 Rag0n

same issue

Nader-CS avatar Sep 23 '24 08:09 Nader-CS

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)

mannoeu avatar Oct 02 '24 13:10 mannoeu

same problem

ShchipskiiYura avatar Oct 08 '24 09:10 ShchipskiiYura

same issue

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

imamrobani avatar Oct 10 '24 05:10 imamrobani

any help ?

Nader-CS avatar Oct 16 '24 20:10 Nader-CS

Same issue here.

39otrebla avatar Oct 22 '24 11:10 39otrebla

Same issue here...

DarshanSavaliyaGitHub avatar Oct 29 '24 04:10 DarshanSavaliyaGitHub

We are having the same issue here:

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

marcos-cunha-gringo avatar Oct 31 '24 19:10 marcos-cunha-gringo

any updates ?

Nader-CS avatar Dec 03 '24 14:12 Nader-CS

any updates ?

Unfortunately, no one can reproduce the error concisely. :(

mannoeu avatar Dec 03 '24 23:12 mannoeu

Has the camerax version has been updated to 1.4.0 ?

I am also facing this issue post version update & have raised it here. Please upvote if the issue is same

Have raised it here https://issuetracker.google.com/issues/383220395

UKMIITB avatar Dec 10 '24 09:12 UKMIITB

Has the camerax version has been updated to 1.4.0 ?

https://github.com/mrousavy/react-native-vision-camera/blob/54df86ace1716039f690e865c97f29f87dfe43b7/package/android/build.gradle#L185-L192

mrousavy avatar Dec 10 '24 11:12 mrousavy

@mrousavy We are using version 4.6.1, and our build.gradle file is the same as the one you provided, 1.5.0-alpha03. However, some phones are still reporting this error.

hongying-yang avatar Dec 11 '24 03:12 hongying-yang

still same

Nader-CS avatar Dec 11 '24 12:12 Nader-CS

still same, we get this error on some devices. do you have any suggestions for a solution?

hozdemir avatar Dec 24 '24 12:12 hozdemir

Still same. Is there a solution for this?

kadiraydinli avatar Dec 27 '24 11:12 kadiraydinli

any updates ? thie issue happen today for 15 user.

Nader-CS avatar Dec 27 '24 17:12 Nader-CS

I am also getting this crash in firebase crashlytics

JuhilGajeraGitHub avatar Jan 01 '25 05:01 JuhilGajeraGitHub

So do i

levepic avatar Jan 06 '25 12:01 levepic

+1 Same issue

litinskii avatar Jan 12 '25 09:01 litinskii

i am also getting the same error. -- i am not able to reproduce exactly but getting this log from some devices listed below with OS version

  • here's some stacktrace from dynatrace:

details: "react-native": "0.75.4", "react-native-vision-camera": "^4.5.3",

Devices P30 Pro (46.6 %) Galaxy A51 (26.2 %) HUAWEI nova 5T (3.4 %) Infinix HOT 40i (2.43 %) Others (21.4 %)

Operating system Android 10 (50 %) Android 13 (31.1 %) Android 11 (6.31 %) Android 12 (4.37 %) Others (8.25 %)

`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.processTask(ThreadPoolExecutor.java:1187)

at

java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1152)

at

java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)

at

java.lang.Thread.run(Thread.java:784)`

rb-adnan avatar Jan 12 '25 14:01 rb-adnan

any updates ?!

Nader-CS avatar Jan 15 '25 19:01 Nader-CS