Skip to content
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

Returned path string of saveFile() on Android does not resolve to the actual selected file path #1553

Closed
2 of 4 tasks
DarkEnigma opened this issue Jul 16, 2024 · 7 comments
Closed
2 of 4 tasks
Labels

Comments

@DarkEnigma
Copy link

Describe the bug
When using the saveFile() method of the file picker, the given file data is correctly saved to the device storage, but the returned string path is a completely different one. This way it is not possible to do any additional tasks on the written file (in our case some data validation - other tasks, like save the file path in a local data base for later use, is also possible).

Platform

  • Android
  • iOS (potentially)
  • Web
  • Desktop

Platform OS version
Android SDK 33 - tested on SUNMI D3 MINI (Real-Device), Emulator
iOS ?

How are you picking?

final json = {'key': 'value'};
final jsonString = jsonEncode(json);
final data = utf8.encode(jsonString);

final filePath = await FilePicker.platform.saveFile(
  fileName: 'export-${DateTime.now().toIso8601String()}.json',
  type: FileType.custom,
  allowedExtensions: ['json'],
  bytes: data, // type Uint8List

  print(filePath); // /storage/emulated/0/Downloads/<fileName>
);

Details to reproduce the issue
Use the code above

Flutter Version details
Flutter (Channel stable, 3.19.6, on Microsoft Windows [Version 10.0.22631.3880], locale de-DE)
• Flutter version 3.19.6 on channel stable at C:\Tools\flutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 54e66469a9 (3 months ago), 2024-04-17 13:08:03 -0700
• Engine revision c4cd48e186
• Dart version 3.3.4
• DevTools version 2.31.1

Additional context
flutter_file_picker v. 8.0.5

As I further investigated the code and the pull request, that added the saveFile() functionality on mobile, it seems that the returned path is constructed using the environment's download directory (line 91) and just adds the file name retrieved from the data uri. But to write the data to the file, the entire uri seems to be used in the outputStream (line 94).

String path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS)
.getAbsolutePath() + File.separator + FileUtils.getFileName(uri, this.activity);
try {
OutputStream outputStream = this.activity.getContentResolver().openOutputStream(uri);
if(outputStream != null){
outputStream.write(bytes);
outputStream.flush();
outputStream.close();
}
finishWithSuccess(path);

As I am relatively new to Flutter (less than a year of experience on this topic) and even less on Android java/kotlin I unfortunately don't know a possible solution. Maybe it is possible to use additional information of the uri in some way to resolve a functioning path. Thanks in advance for the support and thank you for providing this otherwise cool package.

Copy link

This issue is stale because it has been open for 7 days with no activity.

@github-actions github-actions bot added the stale label Jul 24, 2024
@DarkEnigma
Copy link
Author

DarkEnigma commented Jul 25, 2024

Anyone an idea or the time to solve this?

@github-actions github-actions bot removed the stale label Jul 26, 2024
Copy link

github-actions bot commented Aug 2, 2024

This issue is stale because it has been open for 7 days with no activity.

@github-actions github-actions bot added the stale label Aug 2, 2024
@deathblade666
Copy link

deathblade666 commented Aug 5, 2024

I am also seeing this issue, where regardless of the path selected the return value using the .save() function is always /storage/emulated/0/Downloads/. interestingly, if you pass through the bytes argument to the save() it does right to an existing file, however it will not if the file size is more than the bytes you are trying to write (i.e. if content was removed from the file)

edit: Opened a separate issue for the second half of this #1569

@github-actions github-actions bot removed the stale label Aug 6, 2024
Copy link

This issue is stale because it has been open for 7 days with no activity.

@github-actions github-actions bot added the stale label Aug 13, 2024
Copy link

This issue was closed because it has been inactive for 14 days since being marked as stale.

@lumpsoid
Copy link

lumpsoid commented Sep 4, 2024

if there is no easy way to solve it, how to use saveFile() method without dialog? because there is no point in it

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants