Im trying to add a camera and take a picture option in a flutter app. Using an android emulator when I load the app, it instantly crashes and gives out some errors.
I used the camera.dart library and heres what my build Widget build looks like
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: const Text('Take a picture')),
// You must wait until the controller is initialized before displaying the
// camera preview. Use a FutureBuilder to display a loading spinner until the
// controller has finished initializing.
body: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
// If the Future is complete, display the preview.
return CameraPreview(_controller);
} else {
// Otherwise, display a loading indicator.
return const Center(child: CircularProgressIndicator());
}
},
),
floatingActionButton: FloatingActionButton(
// Provide an onPressed callback.
onPressed: () async {
// Take the Picture in a try / catch block. If anything goes wrong,
// catch the error.
try {
// Ensure that the camera is initialized.
await _initializeControllerFuture;
// Attempt to take a picture and get the file `image`
// where it was saved.
final image = await _controller.takePicture();
if (!context.mounted) return;
// If the picture was taken, display it on a new screen.
await Navigator.of(context).push(
MaterialPageRoute(
builder: (context) => DisplayPictureScreen(
// Pass the automatically generated path to
// the DisplayPictureScreen widget.
imagePath: image.path,
),
),
);
} catch (e) {
// If an error occurs, log the error to the console.
print(e);
}
},
child: const Icon(Icons.camera_alt),
),
);
}
}
and this is how the camera is initialised
void initState() {
super.initState();
// To display the current output from the Camera,
// create a CameraController.
_controller = CameraController(
// Get a specific camera from the list of available cameras.
widget.camera,
// Define the resolution to use.
ResolutionPreset.medium,
);
// Next, initialize the controller. This returns a Future.
_initializeControllerFuture = _controller.initialize();
}
I have set the back camera to webcam0 in avd settings and also checked the available cameras which returns my camera on my mac. The app works if its a emulated camera but crashes the moment its a real camera. Theres a few issues but I think they can be solved by changing one or two things which are to do with flutter finding the camera itself.
What do I need to change about my camera settings to get the app to use the camera properly.
Here is the error log
[ERR] The Android emulator exited with code -4 after startup
[ERR] Android emulator stderr:
[ERR] WARNING | cannot add library /Users/naeemujeeb/Library/Android/sdk/emulator/qemu/darwin-aarch64/lib64/vulkan/libvulkan.dylib: failed
[ERR] WARNING | *** No gRPC protection active, consider launching with the -grpc-use-jwt flag.***
[ERR] [11764:103316:20240322,183036.876846:WARNING crash_report_exception_handler.cc:235] UniversalExceptionRaise: (os/kern) failure (5)
[ERR] Address these issues and try again.
[ERR] Error 1 retrieving device properties for sdk gphone64 arm64:
[ERR] adb: device 'emulator-5554' not found
[ERR] Error 1 retrieving device properties for sdk gphone64 arm64:
[ERR] adb: device 'emulator-5554' not found
[ERR] The Android emulator exited with code -4 after startup
[ERR] Android emulator stderr:
[ERR] WARNING | cannot add library /Users/naeemujeeb/Library/Android/sdk/emulator/qemu/darwin-aarch64/lib64/vulkan/libvulkan.dylib: failed
[ERR] WARNING | *** No gRPC protection active, consider launching with the -grpc-use-jwt flag.***
[ERR] [12966:113491:20240322,183623.961294:ERROR directory_reader_posix.cc:42] opendir /tmp/android-naeemujeeb/emu-crash.db/attachments/0727ce45-6131-437c-91c6-eb39d264bc86: No such file or directory (2)
[ERR] [12966:113490:20240322,183624.089613:WARNING crash_report_exception_handler.cc:235] UniversalExceptionRaise: (os/kern) failure (5)
[ERR] Address these issues and try again.