如何修复 Android 上的错误“ERROR_CAMERA_DISABLED”?

问题描述 投票:0回答:1

我正在使用 Kotlin 和 Android Studio 开发一个 Android 应用程序,该应用程序打开相机并启动 setRepeatingRequest。 该应用程序使用“CameraDevice.StateCallback”、“CameraCaptureSession.StateCallback”和“ImageReader.OnImageAvailableListener”。

如果我保持屏幕打开,应用程序永远不会崩溃。 如果我锁定屏幕,或者让它锁定,大约一分钟后,我会收到两次错误“ERROR_CAMERA_DISABLED”[1]。

我需要做什么才能避免出现错误?

[1]:我在“CameraDevice.StateCallback”上设置“onError”来打印错误代码和字符串“onError”,我在 Logcat 中看到该消息两次,如下所示:

MyCamera    com.example.app3    D  3
MyCamera    com.example.app3    D  onError
MyCamera    com.example.app3    D  3
MyCamera    com.example.app3    D  onError

我试图通过定义“finalize”函数来查看是否有东西被垃圾收集,但没有成功。

android kotlin android-camera2
1个回答
-2
投票
The "ERROR_CAMERA_DISABLED" issue you're encountering on Android can be related to the camera being closed or disabled unexpectedly. Here are a few steps you can take to troubleshoot and potentially fix this issue:

Check Camera Permissions:
Ensure that your application has the necessary permissions to use the camera. Add the following permission to your AndroidManifest.xml file:

xml

    <uses-permission android:name="android.permission.CAMERA" />

Also, make sure you are checking and requesting the camera permission at runtime, as permissions might be revoked when the device is locked.

Handle Camera State Changes:
Implement proper handling of camera state changes in your CameraDevice.StateCallback. When the screen is locked, the camera device might be closed, and you should be prepared to reopen it when needed. Check for the ERROR_CAMERA_DEVICE state and handle it appropriately.

kotlin

    override fun onDisconnected(camera: CameraDevice) {
        // Handle camera disconnection. Reopen the camera if needed.
    }
    
    override fun onError(camera: CameraDevice, error: Int) {
        // Handle camera errors. Reopen the camera if needed.
    }

Manage the Camera Lifecycle:
Ensure that you are properly managing the camera's lifecycle. Open the camera when your application starts and close it when your application is paused or destroyed. You may want to reopen the camera when the application comes back to the foreground.

Wake Locks:
When the screen is locked, the device might be going into a low-power state, causing the camera to be disabled. Consider using a wake lock to prevent the device from going to sleep while your camera functionality is active. Remember to release the wake lock when you're done.

kotlin

    val powerManager = getSystemService(Context.POWER_SERVICE) as PowerManager
    val wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "MyCameraApp:WakeLock")
    wakeLock.acquire()

// ... camera operations ...
wakeLock.release()
Investigate Camera Configuration:
Check if the issue is specific to certain camera configurations. You might want to test with different camera parameters and configurations to see if the problem persists.

Update Camera Dependencies:
Ensure that you are using the latest versions of camera-related dependencies. Check for any updates to the CameraX library or other camera-related libraries that you might be using.

Remember to test your application on different devices and Android versions to ensure compatibility. If the issue persists, you may want to consult the official Android documentation, check for relevant GitHub issues, or seek help on forums like Stack Overflow for more specific guidance.


  [1]: https://www.bigscal.com/android-app-development-services/
© www.soinside.com 2019 - 2024. All rights reserved.