Home > Back-end >  Can't save video to Application directory - Flutter
Can't save video to Application directory - Flutter

Time:05-15

I am working on an app where I want to record a video to file with the timestamp as name. As I understand it you should be able to use the returned XFile and use the saveTo function. I get an error that does seem odd to me.

Here is the code where I save the video:

void onStopRecordingButtonPressed() {
    stopVideoRecording().then((video) async {
      if (video != null) {
        final path = await _localPath;
        final file = File('$path/${timestamp()   ".mp4"}');
        await file.create(recursive: true);
        print("video path "   video.path.toString());
        print("VideoFile: "   video.name.toString());
        print("Desired path: "   file.toString());
        var fileExists = await file.exists();
        if (fileExists) {
          print("fileExists");
        } else {
          print("No, this does not exist");
        }
        video.saveTo(file.toString());
      }
    });
  }


Future<XFile?> stopVideoRecording() async {
    final CameraController? cameraController = controller;
    if (cameraController == null || !cameraController.value.isInitialized) {
      return null;
    }

    try {
      return cameraController.stopVideoRecording();
    } on CameraException catch (e) {
      _showCameraException(e);
      return null;
    }
  }

and this is the output:

flutter: video path /var/mobile/Containers/Data/Application/3339192C-B309-4E08-8017-FFBD735CA576/Documents/camera/videos/REC_4525E32A-81C0-42BA-A3A4-48E325E44E58.mp4
flutter: VideoFile: REC_4525E32A-81C0-42BA-A3A4-48E325E44E58.mp4
flutter: Desired path: File: '/var/mobile/Containers/Data/Application/3339192C-B309-4E08-8017-FFBD735CA576/Documents/1652533015054.mp4'
flutter: fileExists <-- Exists
open on File: '/var/mobile/Containers/Data/Application/3339192C-B309-4E08-8017-FFBD735CA576/Documents/1652533015054.mp4': No such file or directory <-- So how can this be?
Application finished.

CodePudding user response:

XFile.toString() is the string representation of the object, not a path.

File: '/var/mobile/Containers/Data/Application/3339192C-B309-4E08-8017-FFBD735CA576/Documents/1652533015054.mp4'

Try something like this:

video.saveTo(file.path);
  • Related