始终使用flutter下载程序包下载失败

时间:2019-02-11 09:25:45

标签: flutter

我尝试使用flutter下载程序包从Firebase存储下载pdf文件。但是下载总是显示失败。我不知道是什么原因。

这是我尝试的代码。请提出一些解决方案。谢谢。

void _startDownload(String link) async
{
   var dir = await getExternalStorageDirectory();
   try{
      final taskId = await FlutterDownloader.enqueue(
      url: link,
      savedDir:  dir.path,
      fileName:"sampe.docx",
      showNotification: true, // show download progress in status bar(forAndroid)
      openFileFromNotification: true, // click on notification to open downloaded file (for Android)
      );
      debugPrint(taskId);

   //   FlutterDownloader.retry(taskId: taskId);
  }
  catch(e){
     print(e);
  }
}

这是我收到的错误。

W/System.err(18012): java.io.FileNotFoundException: /storage/emulated/0/sampe.docx (Permission denied)
W/System.err(18012):    at java.io.FileOutputStream.open(Native Method)
W/System.err(18012):    at java.io.FileOutputStream.<init>(FileOutputStream.java:221)
W/System.err(18012):    at java.io.FileOutputStream.<init>(FileOutputStream.java:140)
W/System.err(18012):    at vn.hunghd.flutterdownloader.DownloadWorker.downloadFile(DownloadWorker.java:190)
W/System.err(18012):    at vn.hunghd.flutterdownloader.DownloadWorker.doWork(DownloadWorker.java:102)
W/System.err(18012):    at androidx.work.Worker$1.run(Worker.java:57)
W/System.err(18012):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
W/System.err(18012):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
W/System.err(18012):    at java.lang.Thread.run(Thread.java:760)
I/WorkerWrapper(18012): Worker result SUCCESS for Work [ id=b7676538-a655-454a-89e2-1899f0e078cd, tags={ vn.hunghd.flutterdownloader.DownloadWorker, flutter_download_task } ]

3 个答案:

答案 0 :(得分:0)

确保您具有写权限。不读,不写

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

答案 1 :(得分:0)

尝试将 android:requestLegacyExternalStorage="true" 添加到 flutter_downloader 中的 android/app/src/main/AndroidManifest.xml 提供程序,例如:

<provider
        android:name="vn.hunghd.flutterdownloader.DownloadedFileProvider"
        android:authorities="im.mingguang.mingguang_app.flutter_downloader.provider"
        android:exported="false"
        android:grantUriPermissions="true"
        android:requestLegacyExternalStorage="true">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/provider_paths"/>
</provider>

答案 2 :(得分:0)

//Check permission first need check permission
  Future<bool> _checkPermission() async {
if (platform == TargetPlatform.android) {
  final status = await Permission.storage.status;
  if (status != PermissionStatus.granted) {
    final result = await Permission.storage.request();
    if (result == PermissionStatus.granted) {
      return true;
    }
  } else {
    return true;
  }
} else {
  return true;
}
return false;
}
// user_permission code for mainfest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>