Flutter拍照触发Session 0停止重复捕获异常(API 30模拟器)
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:
- Android 11 Permission Changes: API 30 introduced the
ACCESS_MEDIA_LOCATIONpermission, and older versions of the Flutter camera plugin don't handle this correctly. - 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:
- Open the emulator's extended controls (three-dot button).
- Go to Camera settings.
- Set both front and back cameras to your physical webcam (instead of "Virtual Scene").
- 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

