I'm developing a mobile app using flutter. For that I used downloads-path-provider to get the download directory of the mobile phone. The emulator returns /storage/emulated/0/Download
. Also when I save a file in this directory the file can be visible in the Download folder.
But on a real devices it also returns the same directory path. /storage/emulated/0/Download
Is this correct for the actual devices? Because on actual devices I cannot see the saved file in the Download folder.
Is there any solution to find the downloads directory on a real device?
Show activity on this post. then use these 2 packages: permission_handler to request storage permission. downloads_path_provider to save the file in downloads directory.
add this to manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
then use these 2 packages:
in pubspec add:
permission_handler: ^5.0.1+1
downloads_path_provider: ^0.1.0
then
Future<File> writeFile(Uint8List data, String name) async {
// storage permission ask
var status = await Permission.storage.status;
if (!status.isGranted) {
await Permission.storage.request();
}
// the downloads folder path
Directory tempDir = await DownloadsPathProvider.downloadsDirectory;
String tempPath = tempDir.path;
var filePath = tempPath + '/$name';
//
// the data
var bytes = ByteData.view(data.buffer);
final buffer = bytes.buffer;
// save the data in the path
return File(filePath).writeAsBytes(buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
}
then use the method:
var file = await writeFile(uint8List, 'name');
path_provider will probably undergo some changes soon, there are some open issues:
https://github.com/flutter/flutter/issues/35783
As of right now, the best way to get the download path on an Android device is to use:
/storage/emulated/0/Download/
No (s) needed.
And to get the external dir path in Android:
/storage/emulated/0/
The "emulated" word does not mean it's the emulator path, it's just a naming convention.
Make sure you have permission to write to the file, add this to manifest.xml file, right under <manifest tag:
<manifest package="..." ... >
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
and also request permission at run time.
See https://pub.dev/packages/permission_handler
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With