Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Cannot open file, path = . flutter

I’m trying to take a photo and save it to a custom directory in phone(or at least somewhere i can save it as .jpg or .jpeg).

I’m able to take photo but when I reach the DisplayPictureScreen page, flutter can not read the photo from directory which I defined.

here’s codes:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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


here is full error log :

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

I don’t know where is the problem, when i use image.path as directory it works fine but i can’t find the photo in my directory.

>Solution :

Here is your code:

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(
  ...
);

On the first line, you declare the name of the folder you want to use.
Then you create said folder.

Then on the third line you declare the name of the file you want to use.

Then you take the picture, and immediately call Navigator.of, you never tell the picture it should be saved on your file path, all you do is declare the filepath variable.

I believe you can use the .saveTo method on the image:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading