flutter / flutter/flutter

`takePicture()` not working if called after `pausePreview()`

Open
#176,288 6 comments 0 reactions 0 assignees View on GitHub
found in release: 3.35 found in release: 3.37 has reproducible steps p: camera P3 package platform-android team-android triaged-android
Dominant language
Dart
Stars
179k
Forks
31.1k
PR merge metrics
PR metrics pending

Description

### 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 createState() => _CameraPageState();
}

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

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

initializer = initCamera();
}

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

Future 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 _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 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.

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.