簡體   English   中英

在flutter上使用最新版本的camera包時如何修復錯誤

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

我是一個新手開發人員,正在開發。 我正在使用相機包開發過濾器應用程序。 您使用的軟件包版本是相機:^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());
          }
        },
      ),
    );
  }
}

我修好了它。 照片開始正常拍攝。 希望使用最新版相機包的人參考一下。

            _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