8000 camera plugin - saveTo() completes before file lock is released sometimes · Issue #72652 · flutter/flutter · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
darre1 opened this issue Dec 19, 2020 · 4 comments · Fixed by flutter/plugins#3363
Closed
Labels
p: camera The camera plugin p: cross_file The cross_file plugin package flutter/packages repository. See also p: labels.

Comments

@darre1
Copy link
darre1 commented Dec 19, 2020

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.

@mvanbeusekom mvanbeusekom added p: cross_file The cross_file plugin p: camera The camera plugin labels Dec 20, 2020
@pedromassangocode
Copy link

Hi @darre1
Can you please provide your flutter doctor -v the steps to reproduce the issue and a minimal complete reproducible code sample?
Thank you

@pedromassangocode pedromassangocode added waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds in triage Presently being triaged by the triage team labels Dec 21, 2020
@mvanbeusekom
Copy link

@darre1 the saveTo method is declared as void saveTo(String path) async { ... }. Since it has the async modifier you can still await it and make sure the method finishes saving before you load it into your Image widget.

I understand that this is not really clear to users of the XFile and it would be better to have the saveTo return a Future<void>. Therefore I created a pull request to address this.

@pedromassangocode pedromassangocode added p: first party and removed in triage Presently being triaged by the triage team waiting for customer response The Flutter team cannot make further progress on this issue until the original reporter responds labels Dec 22, 2020
@darre1
Copy link
Author
darre1 commented Dec 22, 2020

That makes sense. Thanks.

I get this warning in my IDE when doing that. I guess the pull request will resolve the warning.

image

@github-actions
Copy link
github-actions bot commented Aug 8, 2021

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Aug 8, 2021
@flutter-triage-bot flutter-triage-bot bot added the package flutter/packages repository. See also p: labels. label Jul 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
p: camera The camera plugin p: cross_file The cross_file plugin package flutter/packages repository. See also p: labels.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants
0