how can i use path_provider to download files from bytes in external storage, in ios and android?

68 Views Asked by At

How can I use path_provider to download files from bytes in external storage, like download folder, like you try to make it get the application directory, writing it and moving to external storage, but it doesn't give me error and it don't make the download.

This is how I done the download to application storage:

var tempDir = await getApplicationDocumentsDirectory();
var tempPath = tempDir.path;
var tempFile = File('$tempPath/${solicitacao.id}.pdf');
tempFile.writeAsBytesSync(solicitacao.matriculapdf);
if (selectedPdfs.any((file) => file.path == tempFile.path)) {
  selectedPdfs.remove(tempFile);
} else {
  selectedPdfs.add(tempFile);
}
1

There are 1 best solutions below

2
Jaimin Raval On

User Permission Handler package for permission.

first set the permissions right in your Android Manifest

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

This will work in both Android and IOS.

 Future<String> createFolder(String download) async {
    final dir = Directory((Platform.isAndroid
        ? await getExternalStorageDirectory() //FOR ANDROID
        : await getApplicationSupportDirectory() //FOR IOS
    )!
    .path + '/$download');
var status = await Permission.storage.status;
if (!status.isGranted) {
  await Permission.storage.request();
}
if ((await dir.exists())) {
  return dir.path;
} else {
  dir.create();
  return dir.path;
}}

Now save file into this folder

var tempPath =await createFolder(download);
var tempFile = File('$tempPath/${solicitacao.id}.pdf');
tempFile.writeAsBytesSync(solicitacao.matriculapdf);
  if (selectedPdfs.any((file) => file.path == tempFile.path)) {
     selectedPdfs.remove(tempFile);
   } else {
     selectedPdfs.add(tempFile);
   }

Answer Reference