You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter拍照触发Session 0停止重复捕获异常(API 30模拟器)

Flutter Camera Plugin: CAMERA_ERROR (3) on API 30 Emulator When Taking Photos

Problem Overview

I'm working with the Flutter camera plugin to capture photos, but pressing the capture button triggers a critical camera error only when testing on an API 30 emulator. Here's the full exception log:

Session 0: Exception while stopping repeating:
E/CameraCaptureSession(11374): android.hardware.camera2.CameraAccessException: CAMERA_ERROR (3): The camera device has encountered a serious error
E/CameraCaptureSession(11374): at android.hardware.camera2.impl.CameraDeviceImpl.checkIfCameraClosedOrInError(CameraDeviceImpl.java:2231)
E/CameraCaptureSession(11374): at android.hardware.camera2.impl.CameraDeviceImpl.stopRepeating(CameraDeviceImpl.java:1241)
E/CameraCaptureSession(11374): at android.hardware.camera2.impl.CameraCaptureSessionImpl.close(CameraCaptureSessionImpl.java:578)
E/CameraCaptureSession(11374): at io.flutter.plugins.camera.Camera.closeCaptureSession(Camera.java:471)
E/CameraCaptureSession(11374): at io.flutter.plugins.camera.Camera.close(Camera.java:477)
E/CameraCaptureSession(11374): at io.flutter.plugins.camera.Camera$2.onError(Camera.java:176)
E/CameraCaptureSession(11374): at android.hardware.camera2.impl.CameraDeviceImpl.notifyError(CameraDeviceImpl.java:1629)
E/CameraCaptureSession(11374): at android.hardware.camera2.impl.CameraDeviceImpl.lambda$oDs27OTfKFfK18rUW2nQxxkPdV0(Unknown Source:0)
E/CameraCaptureSession(11374): at android.hardware.camera2.impl.-$$Lambda$CameraDeviceImpl$oDs27OTfKFfK18rUW2nQxxkPdV0.accept(Unknown Source:8)
E/CameraCaptureSession(11374): at com.android.internal.util.function.pooled.PooledLambdaImpl.doInvoke(PooledLambdaImpl.java:278)
E/CameraCaptureSession(11374): at com.android.internal.util.function.pooled.PooledLambdaImpl.invoke(PooledLambdaImpl.java:201)
E/CameraCaptureSession(11374): at com.android.internal.util.function.pooled.OmniFunction.run(OmniFunction.java:97)
E/CameraCaptureSession(11374): at android.os.Handler.handleCallback(Handler.java:938)
E/CameraCaptureSession(11374): at android.os.Handler.dispatchMessage(Handler.java:99)
E/CameraCaptureSession(11374): at android.os.Looper.loop(Looper.java:223)
E/CameraCaptureSession(11374): at android.app.ActivityThread.main(ActivityThread.java:7656)
E/CameraCaptureSession(11374): at java.lang.reflect.Method.invoke(Native Method)
E/CameraCaptureSession(11374): at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
E/CameraCaptureSession(11374): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)

Why This Happens

This error is specific to API 30 emulators for two main reasons:

  1. Android 11 Permission Changes: API 30 introduced the ACCESS_MEDIA_LOCATION permission, and older versions of the Flutter camera plugin don't handle this correctly.
  2. Emulator Camera Instability: API 30 emulators often have buggy virtual camera implementations, which can crash when trying to stop the preview session after capturing a photo.

Fixes to Try

1. Update the Camera Plugin to the Latest Version

Older plugin versions have known API 30 compatibility issues. Update your pubspec.yaml with the latest version:

dependencies:
  camera: ^latest_version

Run flutter pub get to install the update — newer versions include patches for API 30+ camera handling.

2. Add Missing Permissions to AndroidManifest.xml

Ensure your android/app/src/main/AndroidManifest.xml includes all required permissions for API 30:

<!-- Core camera permissions -->
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />

<!-- API 30+ media location access -->
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION" />

3. Improve Camera Session Handling in Code

The error often occurs if the camera session isn't managed properly. Follow these best practices:

  • Check if the camera is initialized before capturing.
  • Avoid disposing the controller prematurely.
  • Wrap capture logic in a try-catch to handle errors gracefully:
Future<void> capturePhoto() async {
  try {
    if (!_cameraController.value.isInitialized) {
      return;
    }
    final image = await _cameraController.takePicture();
    // Process your captured image here
  } catch (e) {
    print('Capture error: $e');
    // Reset the camera if the session crashes
    await _cameraController.initialize();
  }
}

4. Use a Physical Webcam for the Emulator

Virtual cameras in API 30 emulators are unreliable. Switch to a physical webcam:

  1. Open the emulator's extended controls (three-dot button).
  2. Go to Camera settings.
  3. Set both front and back cameras to your physical webcam (instead of "Virtual Scene").
  4. Restart the emulator and test again.

5. Temporary Workaround: Use API 29 Emulator

If all else fails, test your camera functionality on an API 29 emulator — it has more stable camera support while you debug the API 30 issue.


内容的提问来源于stack exchange,提问作者Toni Melki

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.09 18:27:35