无法打开文件,路径 = 。扑

Cannot open file, path = . flutter

我正在尝试拍摄照片并将其保存到 phone 中的自定义目录(或者至少我可以将其保存为 .jpg 或 .jpeg 的某个地方)。

我可以拍照,但是当我到达 DisplayPictureScreen 页面时,flutter 无法从我定义的目录中读取照片。

代码如下:

ElevatedButton(
              onPressed: () async {
                try {
                  await _initializeControllerFuture;
                  final Directory extDir = await getApplicationDocumentsDirectory();
                  final String dirPath = "${extDir.path}/media";
                  await Directory(dirPath).create(recursive: true);
                  final String filePath = "$dirPath/${_timestamp()}.jpeg";

                  final image = await _controller.takePicture();

                  print(filePath);
                  await Navigator.of(context).push(
                    MaterialPageRoute(
                      builder: (context) => DisplayPictureScreen(

                        imagePath: filePath,
                      ),
                    ),
                  );
                } catch (e) {
                  print(e);
                }
              },
              child: Row(
                mainAxisAlignment: MainAxisAlignment.center,
                children: const [Text("take photo"), Icon(Icons.camera)],
              )),


  String _timestamp() =>DateTime.now().millisecondsSinceEpoch.toString();


class DisplayPictureScreen extends StatelessWidget {
  final String imagePath;

  const DisplayPictureScreen({Key? key, required this.imagePath})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    var pageHeight = MediaQuery.of(context).size.height;
    var pageWidth = MediaQuery.of(context).size.width;
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      home: Scaffold(
        appBar: AppBar(title: const Text('Display the Picture')),
        body: SizedBox(
          height: pageHeight,
          width: pageWidth,
          child: Image.file(
            File(
              imagePath,
            ),
            fit: BoxFit.fill,
          ),
        ),
      ),
    );
  }
}


这是完整的错误日志:

The following FileSystemException was thrown resolving an image codec:
Cannot open file, path =
'/data/user/0/com.example.{myappname}/app_flutter/media/1642831675331.jpeg' (OS Error: No such file
or directory, errno = 2)

When the exception was thrown, this was the stack:
#0      _File.open.<anonymous closure> (dart:io/file_impl.dart:356:9)
<asynchronous suspension>
#3      FileImage._loadAsync (package:flutter/src/painting/image_provider.dart:890:29)
<asynchronous suspension>
(elided 2 frames from dart:async)

Path: /data/user/0/com.example.{myappname}/app_flutter/media/1642831675331.jpeg

我不知道问题出在哪里,当我使用 image.path 作为目录时它工作正常但我在我的目录中找不到照片。

这是您的代码:

final String dirPath = "${extDir.path}/media";
await Directory(dirPath).create(recursive: true);
final String filePath = "$dirPath/${_timestamp()}.jpeg";

final image = await _controller.takePicture();

print(filePath);
await Navigator.of(context).push(
  ...
);

在第一行,声明要使用的文件夹的名称。 然后创建所述文件夹。

然后在第三行声明要使用的文件的名称。

然后你拍照,然后立即调用Navigator.of,你永远不会告诉图片它应该保存在你的文件路径中,你所做的只是声明文件路径变量。

我相信你可以在图像上使用.saveTo方法:

...
final image = await _controller.takePicture();
await image.saveTo(filePath);
...