【问题标题】:How to fix error when using the latest version of the camera package on the flutter在flutter上使用最新版本的相机包时如何修复错误
【发布时间】:2021-07-23 01:30:31
【问题描述】:

我是一名新手开发人员,正在使用 plutter 进行开发。我正在使用相机包开发过滤器应用程序。 您使用的包版本是相机:^0.8.1+5。

我是从flutter.dev拿官方的例子来用的,但是问题是图片是第一次运行app的时候拍的,图片已经没有保存在图库里了。仅拍摄并保存第一张照片。

我使用一个包来保存画廊照片,image_gallery_saver: ^1.6.9

以下是代码部分。有什么问题? 我在这上面花了一个星期,但我不知道。 我需要专家的帮助。 谢谢

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

【问题讨论】:

    标签: flutter camera


    【解决方案1】:

    我修好了。 开始正确拍摄照片。希望使用最新版相机包的朋友参考一下。

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

    【讨论】:

      猜你喜欢
      • 2022-06-27
      • 2023-03-31
      • 1970-01-01
      • 2019-08-11
      • 2022-06-17
      • 2020-10-03
      • 2017-09-08
      • 2022-01-13
      • 2020-07-16
      相关资源
      最近更新 更多