Reputation: 1
I'm a novice developer who is developing with a plutter. I'm developing a filter app using a camera package. The package version you are using is camera: ^0.8.1+5.
I take the official example from flutter.dev and use it, but the problem is that the picture is taken the first time when running the app, and the picture is no longer saved in the gallery. Only the first picture is taken and saved.
I use a package for saving gallery photos, image_gallery_saver: ^1.6.9
The following is the code part. What could be wrong? I've been spending a week on this, but I don't know. I need the help of the experts. Thanks
class _TakePictureScreenState extends State<TakePictureScreen> {
late CameraController _controller;
late Future<void> _initializeControllerFuture;
XFile? imageFile;
@override
void initState() {
super.initState();
_controller = CameraController(
widget.camera,
ResolutionPreset.high,
);
_initializeControllerFuture = _controller.initialize();
}
@override
void dispose() {
_controller.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: FutureBuilder<void>(
future: _initializeControllerFuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.done) {
return Container(
child: CameraPreview(_controller),
);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
},
),
floatingActionButton: FloatingActionButton(
onPressed: () async {
try {
await _initializeControllerFuture;
final path = join(
(await getApplicationDocumentsDirectory()).path,
'${DateTime.now()}.png',
);
imageFile = await _controller.takePicture();
imageFile!.saveTo(path);
Fluttertoast.showToast(msg: 'take Picture');
ImageGallerySaver.saveFile(path);
// Navigator.push(
// context,
// MaterialPageRoute(
// builder: (context) => DisplayPictureScreen(imagePath: path),
// ),
// );
} catch (e) {
Fluttertoast.showToast(msg: e.toString());
}
},
),
);
}
}
Upvotes: 0
Views: 407
Reputation: 1
I fixed it. The picture is starting to be taken properly. I hope people who use the latest version of the camera package refer to it.
_controller.takePicture().then((XFile? file) {
if (mounted) {
setState(() {
imageFile = file;
});
if (file != null)
Fluttertoast.showToast(msg: 'Picture saved to ${file.path}');
ImageGallerySaver.saveFile(file!.path);
}
});
Upvotes: 0