Skip to content Skip to sidebar Skip to footer

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/Downl

Solution 1:

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:

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

and also request permission at run time.

See https://pub.dev/packages/permission_handler

Solution 2:

add this to manifest:

<uses-permissionandroid: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+1downloads_path_provider: ^0.1.0

then

Future<File> writeFile(Uint8List data, String name) async {
    // storage permission askvar status = awaitPermission.storage.status;
    if (!status.isGranted) {
      awaitPermission.storage.request();
    }
    // the downloads folder pathDirectory tempDir = awaitDownloadsPathProvider.downloadsDirectory;
    String tempPath = tempDir.path;
    var filePath = tempPath + '/$name';
    // // the datavar bytes = ByteData.view(data.buffer);
    final buffer = bytes.buffer;
    // save the data in the pathreturnFile(filePath).writeAsBytes(buffer.asUint8List(data.offsetInBytes, data.lengthInBytes));
  }

then use the method:

var file = await writeFile(uint8List, 'name');

Post a Comment for "Flutter - Save File To Download Folder - Downloads_path_provider"