简体   繁体   English

在flutter上使用最新版本的camera包时如何修复错误

[英]How to fix error when using the latest version of the camera package on the flutter

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.您使用的软件包版本是相机:^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.我从flutter.dev上拿了官方的例子,使用了,但是问题是第一次运行app的时候拍的,图片不再保存在图库里了。 Only the first picture is taken and saved.仅拍摄并保存第一张照片。

I use a package for saving gallery photos, image_gallery_saver: ^1.6.9我使用一个包来保存画廊照片,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());
          }
        },
      ),
    );
  }
}

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);
          }
        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM