Home > Back-end >  Take a photo and saving the photo to storage fails
Take a photo and saving the photo to storage fails

Time:08-27

I am using the camera plugin to take a picture like this:

image = await cameraController.takePicture()

It is working great and I can display this image in a preview. Now I want to store the picture to the device, and I am trying this:

Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
String appDocumentsPath = appDocumentsDirectory.path;
String filePath = '$appDocumentsPath/images/${image!.name}';
await File(image!.path).copy(filePath);

Then I get this error:

Unhandled Exception: FileSystemException: Cannot copy file to '/data/user/0/dk.hempelfonden.frontier/app_flutter/images/CAP8310428751941012175.jpg', path = '/data/user/0/dk.hempelfonden.frontier/cache/CAP8310428751941012175.jpg' (OS Error: No such file or directory, errno = 2)

What am I missing here?

Thank you
Søren

CodePudding user response:

path is the directory /data/user/0/dk.some.app/cache/CAP7304644252296628120.jpg. Try adding a /filename.jpg or .png to it.

Directory appDocumentsDirectory = await getApplicationDocumentsDirectory();
String appDocumentsPath = appDocumentsDirectory.path;
String filePath = '$appDocumentsPath/images/$name.png';
logDebug('Saving from: ${_image!.path}');
logDebug('Copy to: $filePath');
return await File(_image!.path).copy(filePath ".jpg");

CodePudding user response:

I think you trying to copy .png file to .jgp path that is why you are facing FileSystemException try same image type for both image paths.

CodePudding user response:

I was trying to copy the image file to a directory that was not existing, added this code:

String dirPath = '$appDocumentsPath/images';
await Directory(dirPath).create(recursive: true);

CodePudding user response:

you have not created the file. so thats why its show you error

File newFile = File(await filePath);
   
if (newFile.existsSync()) {
  await newFile.delete(); // Delete File first
}
newFile.createSync(recursive: true); // Create Again

  
  • Related