Skip to content

[Camera] UnsupportedOperationException: ProxyApiRegistrar.getDisplay crash on Android 14 with camera 0.11.2 #176613

@hungbui1204

Description

@hungbui1204

Steps to reproduce

Camera plugin (version 0.11.2) crashes with UnsupportedOperationException when initializing front camera with low resolution preset on Android 14. The crash occurs in ProxyApiRegistrar.getDisplay() and DeviceOrientationManager.getDisplay() during orientation handling, detected through Firebase Crashlytics.
Specific device info: Sony Xperia V, Android 14

Expected results

Front camera should initialize successfully with low resolution preset and handle orientation changes without crashing.

Actual results

App crashes with UnsupportedOperationException: Tried to obtain display from a Context not associated with one. Only visual Contexts (such as Activity or one created with Context#createWindowContext) or ones created with Context#createDisplayContext are associated with displays.

Code sample

Code sample
import 'package:flutter/material.dart';
import 'package:camera/camera.dart';

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  runApp(MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: CameraScreen(),
    );
  }
}

class CameraScreen extends StatefulWidget {
  @override
  _CameraScreenState createState() => _CameraScreenState();
}

class _CameraScreenState extends State<CameraScreen> {
  CameraController? controller;
  ValueNotifier<bool> isCameraInitialized = ValueNotifier<bool>(false);

  @override
  void initState() {
    super.initState();
    _initializeCamera();
  }

  void _initializeCamera() async {
    try {
      final cameras = await availableCameras();
      final frontCamera = cameras.firstWhere((camera) {
        return camera.lensDirection == CameraLensDirection.front;
      });

      controller = CameraController(frontCamera, ResolutionPreset.low,
          enableAudio: false);

      await controller!.initialize();

      isCameraInitialized.value = true;

      if (mounted) {
        setState(() {});
      }
    } catch (e) {
      print('Camera initialization error: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text('Camera Test')),
      body: ValueListenableBuilder<bool>(
        valueListenable: isCameraInitialized,
        builder: (context, isInitialized, child) {
          if (isInitialized && controller?.value.isInitialized == true) {
            return CameraPreview(controller!);
          } else {
            return const Center(
              child: CircularProgressIndicator(),
            );
          }
        },
      ),
    );
  }

  @override
  void dispose() {
    controller?.dispose();
    isCameraInitialized.dispose();
    super.dispose();
  }
}

Screenshots or Video

Logs

Logs
ProxyApiRegistrar.getDisplay
java.lang.UnsupportedOperationException - Tried to obtain display from a Context not associated with one. Only visual Contexts (such as Activity or one created with Context#createWindowContext) or

Fatal Exception: java.lang.UnsupportedOperationException
Tried to obtain display from a Context not associated with one. Only visual Contexts (such as Activity or one created with Context#createWindowContext) or ones created with Context#createDisplayContext are associated with displays. Other types of Contexts are typically related to background entities and may return an arbitrary display.

Fatal Exception: java.lang.UnsupportedOperationException: Tried to obtain display from a Context not associated with one. Only visual Contexts (such as Activity or one created with Context#createWindowContext) or ones created with Context#createDisplayContext are associated with displays. Other types of Contexts are typically related to background entities and may return an arbitrary display.
       at android.app.ContextImpl.getDisplay(ContextImpl.java:3025)
       at android.content.ContextWrapper.getDisplay(ContextWrapper.java:1192)
       at io.flutter.plugins.camerax.ProxyApiRegistrar.getDisplay(ProxyApiRegistrar.java:148)
       at io.flutter.plugins.camerax.DeviceOrientationManager.getDisplay(DeviceOrientationManager.java:198)
       at io.flutter.plugins.camerax.DeviceOrientationManager.getDefaultRotation(DeviceOrientationManager.java:185)
       at io.flutter.plugins.camerax.DeviceOrientationManager.getUiOrientation(DeviceOrientationManager.java:150)
       at io.flutter.plugins.camerax.DeviceOrientationManager.handleUiOrientationChange(DeviceOrientationManager.java:98)
       at io.flutter.plugins.camerax.DeviceOrientationManager$1.onOrientationChanged(DeviceOrientationManager.java:74)
       at android.view.OrientationEventListener$SensorEventListenerImpl.onSensorChanged(OrientationEventListener.java:143)
       at android.hardware.SystemSensorManager$SensorEventQueue.dispatchSensorEvent(SystemSensorManager.java:1013)
       at android.os.MessageQueue.nativePollOnce(MessageQueue.java)
       at android.os.MessageQueue.next(MessageQueue.java:335)
       at android.os.Looper.loopOnce(Looper.java:162)
       at android.os.Looper.loop(Looper.java:294)
       at android.app.ActivityThread.main(ActivityThread.java:8223)
       at java.lang.reflect.Method.invoke(Method.java)
       at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:552)
       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:977)

Flutter Doctor output

Doctor output
[✓] Flutter (Channel stable, 3.29.3, on macOS 26.0.1 25A362 darwin-arm64, locale en-VN) [1,781ms]
    • Flutter version 3.29.3 on channel stable at /Users/splus/fvm/versions/3.29.3
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ea121f8859 (6 months ago), 2025-04-11 19:10:07 +0000
    • Engine revision cf56914b32
    • Dart version 3.7.2
    • DevTools version 2.42.3

[✓] Android toolchain - develop for Android devices (Android SDK version 36.0.0) [2.4s]
    • Android SDK at /Users/splus/Library/Android/sdk
    • Platform android-36, build-tools 36.0.0
    • ANDROID_HOME = /Users/splus/Library/Android/sdk
    • Java binary at: /Library/Java/JavaVirtualMachines/jdk-17.jdk/Contents/Home/bin/java
      This JDK is specified in your Flutter configuration.
      To change the current JDK, run: `flutter config --jdk-dir="path/to/jdk"`.
    • Java version Java(TM) SE Runtime Environment (build 17.0.12+8-LTS-286)
    • All Android licenses accepted.

[✓] Xcode - develop for iOS and macOS (Xcode 26.0.1) [1,772ms]
    • Xcode at /Applications/Xcode.app/Contents/Developer
    • Build 17A400
    • CocoaPods version 1.16.2

[✓] Chrome - develop for the web [9ms]
    • Chrome at /Applications/Google Chrome.app/Contents/MacOS/Google Chrome

[✓] Android Studio (version 2024.3) [8ms]
    • Android Studio at /Applications/Android Studio.app/Contents
    • Flutter plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/9212-flutter
    • Dart plugin can be installed from:
      🔨 https://plugins.jetbrains.com/plugin/6351-dart
    • Java version OpenJDK Runtime Environment (build 21.0.6+-13368085-b895.109)

[✓] VS Code (version 1.103.1) [7ms]
    • VS Code at /Applications/Visual Studio Code.app/Contents
    • Flutter extension version 3.120.0

[✓] Connected device (6 available) [9.2s]

[✓] Network resources [810ms]
    • All expected network resources are available.

• No issues found!

Metadata

Metadata

Assignees

Labels

P2Important issues not at the top of the work lista: productionIssues experienced in live production appse: device-specificOnly manifests on certain devicesp: cameraThe camera pluginpackageflutter/packages repository. See also p: labels.platform-androidAndroid applications specificallyteam-androidOwned by Android platform teamtriaged-androidTriaged by Android platform team

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions