Imagine I have created a File
(import 'dart:io
).
I need the user to download the file (to their Download or Documents directory).
This is how I create the file. getApplicationDocumentsDirectory()
is from permission_handler
.
final dir = await getApplicationDocumentsDirectory();
final file = File(
'${dir.path}/myFile.svg');
await file.writeAsString(content);
The file path looks like this on Android:
/data/user/0/com.example.www/app_flutter/myFile.svg
Apparently this is a path that a normal user wouldn’t be able to open.
What I tried so far:
- Opening the local path in a browser so the user can pick a download location themselves (using url_launcher)
- using the
open_file
package to open the file
Edit:
Using the open_file_plus
package like this:
await OpenFile.open(path);
actually allows me to pick an app to open the file (for example the Chrome browser).
But from there the user is still stuck, as no app allows you to "re-download` the file.
2
Answers
Public Android path is:
/storage/emulated/0/Download
My solution use 2 packages, you need to add them to pubspec.yaml:
You can write an helper like this one:
And you need to add theses permission to AndroidManifest:
Then use it like this:
/// you can use the open_file package to open the file with the appropriate app or allow the user to share the file