Skip to content

takePicture() not working if called after pausePreview() #176288

@giordy16

Description

@giordy16

What package does this bug report belong to?

camera

What target platforms are you seeing this bug on?

Android

Have you already upgraded your packages?

Yes

Dependency versions

https://pastebin.com/HRTfQkpX

Steps to reproduce

Run the Sample code. Then click on the button 'Click'. I'm testing on OnePlus 6T (Android 11) and Huawei P20 Pro (Android 10).

Expected results

When clicking on the button 'Click', the camera preview is paused, the picture is taken, and then the camera preview is resumed. Meanwhile the camera preview is paused, a progress indicator is visible.

Actual results

The method takePicture() seems to never give a result.

I've seen that if I do not call onFlashChange in the initState the flow is working as expect, but I need to preset the flash mode before the user can take a picture. Also, if I set the flash mode but don't pause/resume the camera preview, it's also working.

So seems to be some kind of conflict between takePicture, setFlashMode, and pausePreview/resumePreview

Code sample

Code sample
import 'dart:io';

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

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

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  @override
  Widget build(BuildContext context) {
    return MaterialApp(title: 'Flutter Demo', home: CameraPage());
  }
}

class CameraPage extends StatefulWidget {

  @override
  State<CameraPage> createState() => _CameraPageState();
}

class _CameraPageState extends State<CameraPage> {
  late Future initializer;
  late CameraController controller;
  bool progressing = false;

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

    initializer = initCamera();
  }

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

  Future<void> initCamera() async {
    var camera = await availableCameras();
    controller = CameraController(
      camera.first,
      // Set to ResolutionPreset.high. Do NOT set it to ResolutionPreset.max because for some phones does NOT work.
      ResolutionPreset.ultraHigh,
      enableAudio: false,
      imageFormatGroup: Platform.isAndroid
          ? ImageFormatGroup.nv21
          : ImageFormatGroup.bgra8888,
    );
    await controller.initialize();
    await onFlashChange(FlashMode.always);
  }

  Future<void> _takePic() async {
    setState(() {
      progressing = true;
    });

    print("pausePreview");
    await controller.pausePreview();
    print("takePicture");
    final file = await controller.takePicture();
    print("resumePreview");
    await controller.resumePreview();

    print("PATH OF PICTURE: " + file.path);

    setState(() {
      progressing = false;
    });
  }

  Future<void> onFlashChange(FlashMode mode) async {
    print("SETTING FLASH MODE");
    await controller.setFlashMode(mode);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("TEST")),
      body: FutureBuilder(
        future: initializer,
        builder: (context, snap) {
          if (snap.connectionState != ConnectionState.done) {
            return Center(child: CircularProgressIndicator());
          }

          return Column(
            children: [
              Container(height: 400, child: CameraPreview(controller)),
              OutlinedButton(onPressed: _takePic, child: Text("Click")),
              if (progressing) CircularProgressIndicator(),
            ],
          );
        },
      ),
    );
  }
}

Logs

https://pastebin.com/UZr6bAn4

Flutter Doctor output

Doctor output
lucagiordano@Lucas-MacBook-Pro test_app % flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 3.35.5, on macOS 15.3.2 24D81 darwin-arm64, locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 36.1.0)
    ! Some Android licenses not accepted. To resolve this, run: flutter doctor --android-licenses
[✓] Xcode - develop for iOS and macOS (Xcode 16.4)
[✓] Chrome - develop for the web
[✓] Android Studio (version 2025.1)
[✓] VS Code (version 1.104.2)
[✓] Connected device (5 available)
    ! Error: Browsing on the local area network for Hello Bob. Ensure the device is unlocked and attached with a cable or associated with the same local area network as this Mac.
      The device must be opted into Developer Mode to connect wirelessly. (code -27)
[✓] Network resources

! Doctor found issues in 1 category.

Metadata

Metadata

Assignees

No one assigned

    Labels

    P3Issues that are less important to the Flutter projectfound in release: 3.35Found to occur in 3.35found in release: 3.37Found to occur in 3.37has reproducible stepsThe issue has been confirmed reproducible and is ready to work onp: 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