Skip to content
New issue

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

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

Already on GitHub? # to your account

🐛 InitializationException - Device reporting less cameras than anticipated #3179

Closed
3 of 5 tasks
ncoen97 opened this issue Sep 9, 2024 · 37 comments · Fixed by #3385
Closed
3 of 5 tasks

🐛 InitializationException - Device reporting less cameras than anticipated #3179

ncoen97 opened this issue Sep 9, 2024 · 37 comments · Fixed by #3385
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 CameraX@1.5.0-alpha01) 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;

@Nader-CS
Copy link

Nader-CS commented Dec 3, 2024

any updates ?

@mannoeu
Copy link

mannoeu commented Dec 3, 2024

any updates ?

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

@UKMIITB
Copy link

UKMIITB commented Dec 10, 2024

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

@mrousavy
Copy link
Owner

Has the camerax version has been updated to 1.4.0 ?

// CameraX dependency
def camerax_version = "1.5.0-alpha03"
implementation "androidx.camera:camera-core:${camerax_version}"
implementation "androidx.camera:camera-camera2:${camerax_version}"
implementation "androidx.camera:camera-lifecycle:${camerax_version}"
implementation "androidx.camera:camera-video:${camerax_version}"
implementation "androidx.camera:camera-view:${camerax_version}"
implementation "androidx.camera:camera-extensions:${camerax_version}"

@hongying-yang
Copy link

@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.

@Nader-CS
Copy link

still same

@hozdemir
Copy link

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

@kadiraydinli
Copy link

Still same. Is there a solution for this?

@Nader-CS
Copy link

any updates ? thie issue happen today for 15 user.

@JuhilGajeraGitHub
Copy link

I am also getting this crash in firebase crashlytics

@levepic
Copy link

levepic commented Jan 6, 2025

So do i

@litinskii
Copy link

+1 Same issue

@rb-adnan
Copy link

rb-adnan commented Jan 12, 2025

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)`

@Nader-CS

This comment has been minimized.

@UKMIITB
Copy link

UKMIITB commented Jan 16, 2025

Hi everyone,
As mentioned in the above issue tracker that this is an expected behaviour
It was happening previously as well (with older versions), just that it was failing silently.
And now the exception is thrown.

Handling for this is to wrap the cameraProviderFuture.get() call with try catch block
Sample is shared here
https://issuetracker.google.com/issues/383220395#comment3

@mannoeu
Copy link

mannoeu commented Jan 17, 2025

Hi everyone, As mentioned in the above issue tracker that this is an expected behaviour It was happening previously as well (with older versions), just that it was failing silently. And now the exception is thrown.

Handling for this is to wrap the cameraProviderFuture.get() call with try catch block Sample is shared here https://issuetracker.google.com/issues/383220395#comment3

The app will crash? Can i ignore this error?

@mrousavy
Copy link
Owner

created a PR to potentially fix this: #3373

@Nader-CS
Copy link

anyone can confirm if this pr works ?

@rb-adnan
Copy link

@Nader-CS you need fix the variable declaration for provider, then should work fine.

@sorenfrederiksen
Copy link
Contributor

created a PR to potentially fix this: #3373

Awesome! Thanks for looking into this, @mrousavy - really appreciate your hard work 💪

How many people in this long thread are considering supporting Marc's work? We really should be.

sorenfrederiksen added a commit to sorenfrederiksen/react-native-vision-camera that referenced this issue Jan 27, 2025
@sorenfrederiksen
Copy link
Contributor

created a PR to potentially fix this: #3373

Hey @mrousavy,

When you've got a spare moment, can you maybe review my PR aiming to fix this issue? #3385

It follows through on what I think were your intentions, with your PR, and seems to work locally.

(And would be my first contribution to the library, so if you like making people feel special, there's that, too 💫)

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