final imageFile = await File(path); doesn't create a file

Issue

I am using the camera to take a picture and save the picture on phones memory to later send it to AWS. I can take a picture and the path for the picture is there, but I am failing to create a file from the path using:

final imageFile = await File(path);

Why is the file not being created here? I always get ‘Image file does not exist’ on debug console.

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

Solution

takePicture method will return taken image as XFile, you can use the XFile path to create a File with taken Image path like below code.

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

Answered By – MSARKrish

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *