Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter - save file to download folder - downloads_path_provider

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?

like image 465
Amith Avatar asked May 23 '20 02:05

Amith


People also ask

How do I save downloads folder in flutter?

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.


2 Answers

add this to manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

then use these 2 packages:

  1. permission_handler to request storage permission
  2. downloads_path_provider to save the file in downloads directory

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');
like image 114
Abdelrahman Tareq Avatar answered Sep 21 '22 19:09

Abdelrahman Tareq


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

like image 21
live-love Avatar answered Sep 20 '22 19:09

live-love