【问题标题】:final imageFile = await File(path); doesn't create a file最终图像文件 = 等待文件(路径);不创建文件
【发布时间】:2022-01-16 07:29:17
【问题描述】:

我正在使用相机拍照并将照片保存在手机内存中,以便稍后将其发送到 AWS。我可以拍照并且图片的路径在那里,但我无法使用以下路径从路径创建文件:

final imageFile = await File(path);

为什么这里没有创建文件?我总是在调试控制台上得到“图像文件不存在”。

    import 'dart:io';
    import 'package:camera/camera.dart';
    import 'package:flutter/material.dart';
    import 'package:path/path.dart';
    import 'package:path_provider/path_provider.dart';

    class CameraPage extends StatefulWidget {
      // 1
      final CameraDescription? camera;
      // 2
      final ValueChanged? didProvideImagePath;

      CameraPage({Key? key, this.camera, this.didProvideImagePath})
          : super(key: key);

      @override
      State<StatefulWidget> createState() => _CameraPageState();
    }

    class _CameraPageState extends State<CameraPage> {
      CameraController? _controller;
      Future<void>? _initializeControllerFuture;

      @override
      void initState() {
        super.initState();
        // 3
        _controller = CameraController(widget.camera!, ResolutionPreset.medium);
        _initializeControllerFuture = _controller?.initialize();
      }

      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: FutureBuilder<void>(
            future: _initializeControllerFuture,
            builder: (context, snapshot) {
              // 4
              if (snapshot.connectionState == ConnectionState.done) {
                return CameraPreview(this._controller!);
              } else {
                return Center(child: CircularProgressIndicator());
              }
            },
          ),
          // 5
          floatingActionButton: FloatingActionButton(
              child: Icon(Icons.camera), onPressed: _takePicture),
        );
      }

      // 6
      void _takePicture() async {
        try {
          await _initializeControllerFuture;

          final tmpDirectory = await getTemporaryDirectory();
          final filePath = '${DateTime.now().millisecondsSinceEpoch}.jpg';
          final path = join(tmpDirectory.path, filePath);

          await _controller?.takePicture();

          widget.didProvideImagePath!(path);

          final imageFile = await File(path);

          if (imageFile.existsSync())
            print('Image file exists');
          else
            print('Image file does not exist');
        } catch (e) {
          print(e);
        }
      }

      // 7
      @override
      void dispose() {
        _controller?.dispose();
        super.dispose();
      }
    }

【问题讨论】:

  • File(path) 不在文件系统上创建文件。它创建了一个 Dart File 对象,它提供了一个用于读取或写入文件系统的接口。要在文件系统上创建文件,您需要将其写入到磁盘。
  • 另外,await File(path) 没有意义,因为构造函数不能是异步的。

标签: flutter dart path camera dart-io


【解决方案1】:

takePicture 方法会将拍摄的图像返回为 XFile,您可以使用 XFile 路径创建带有拍摄图像路径的文件,如下代码所示。

  File? imageFile;
  XFile? xFile = await _controller?.takePicture();
  if (xFile != null) {
    imageFile = File(xFile.path);
  }

  if (imageFile != null && imageFile.existsSync()) {
    print('Image file exists');
  } else {
    print('Image file does not exist');
  }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-03-23
    相关资源
    最近更新 更多