Skip to content

camera plugin - saveTo() completes before file lock is released sometimes #72652

@darre1

Description

@darre1

Hi

The latest release of the camera plugin (0.6.0) has started using an XFile. the saveTo() function seems to have a problem though where the lock on the file is not always being released before the function completes or something.

For example I was doing this:

XFile file = await _controller.takePicture();
final path = join(
  (await getExternalStorageDirectory()).path,
  '${DateTime.now()}.png',
);
file.saveTo(path);

...and then attempting to display the file on-screen immediately afterwards (like this: Image.file(File(path)). 2 times out of 3 it would work fine but sometimes I would get an error that the file could not be found. If I browse through the phone itself though I will find that the file does exist and I was using the correct path in code (I double-checked everything!).

To get around the problem I am now doing this:

XFile file = await _controller.takePicture();
final path = join(
  (await getExternalStorageDirectory()).path,
  '${DateTime.now()}.png',
);
Uint8List imageBytes = await file.readAsBytes();
await File(path).writeAsBytes(imageBytes);

The above approach doesn't suffer from the same problem, presumably because the Future is not returned by writeAsBytes() until the exclusive lock it has on the file (i.e. while writing to it) has been released.

Metadata

Metadata

Assignees

No one assigned

    Labels

    p: cameraThe camera pluginp: cross_fileThe cross_file pluginpackageflutter/packages repository. See also p: labels.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions