簡體   English   中英

Flutter 中的 flutter_downloader 下載問題

[英]flutter_downloader download issue in Flutter

我正在使用flutter_downloader package download files Flutter中的文件。 文件下載到Downloads文件夾中。 文件下載后通知顯示下載failed to download並拋出以下錯誤。 我被這個錯誤困了將近 2 天,您的幫助將挽救我的一天。

這是代碼:

void _download(String url) async {
    final status = await Permission.storage.request();

    if (status.isGranted) {
      final externalDir = await getExternalStorageDirectory();

      final id = await FlutterDownloader.enqueue(
        fileName: "LRMonoPhase4.mp3",
        url: 'https://www.kozco.com/tech/LRMonoPhase4.mp3',
        savedDir: '/storage/emulated/0/Download',
        showNotification: true,
        openFileFromNotification: true,
      );
      util.showToast(id.toString());
    } else {
      print('Permission Denied');
    }
  }

這是清單

<provider
            android:name="vn.hunghd.flutterdownloader.FlutterDownloaderInitializer"
            android:authorities="${applicationId}.flutter-downloader-init"
            android:exported="false">
            <!-- changes this number to configure the maximum number of concurrent tasks -->
            <meta-data
                android:name="vn.hunghd.flutterdownloader.MAX_CONCURRENT_TASKS"
                android:value="5" />
        </provider>

這是錯誤

D/DownloadWorker( 9655): Update too frequently!!!!, this should be dropped
D/DownloadWorker( 9655): Update too frequently!!!!, this should be dropped
D/EGL_emulation( 9655): app_time_stats: avg=19.11ms min=7.88ms max=35.05ms count=53
D/DownloadWorker( 9655): Update notification: {notificationId: 11, title: LRMonoPhase4.mp3, status: RUNNING, progress: 100}
D/DownloadWorker( 9655): Update too frequently!!!!, but it is the final update, we should sleep a second to ensure the update call can be processed
D/EGL_emulation( 9655): app_time_stats: avg=21.50ms min=8.00ms max=35.47ms count=46
D/DownloadWorker( 9655): Update notification: {notificationId: 11, title: LRMonoPhase4.mp3, status: FAILED, progress: -1}
W/System.err( 9655): java.lang.IllegalArgumentException: Failed to find configured root that contains /storage/emulated/0/Download/LRMonoPhase4.mp3
W/System.err( 9655):    at androidx.core.content.FileProvider$SimplePathStrategy.getUriForFile(FileProvider.java:825)
W/System.err( 9655):    at androidx.core.content.FileProvider.getUriForFile(FileProvider.java:450)
W/System.err( 9655):    at vn.hunghd.flutterdownloader.IntentUtils.buildIntent(IntentUtils.kt:19)
W/System.err( 9655):    at vn.hunghd.flutterdownloader.IntentUtils.validatedFileIntent(IntentUtils.kt:36)
W/System.err( 9655):    at vn.hunghd.flutterdownloader.DownloadWorker.downloadFile(DownloadWorker.kt:445)
W/System.err( 9655):    at vn.hunghd.flutterdownloader.DownloadWorker.doWork(DownloadWorker.kt:208)
W/System.err( 9655):    at androidx.work.Worker$1.run(Worker.java:86)
W/System.err( 9655):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1137)
W/System.err( 9655):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:637)
W/System.err( 9655):    at java.lang.Thread.run(Thread.java:1012)
I/WM-WorkerWrapper( 9655): Worker result FAILURE for Work [ id=69548c69-93cb-47f0-b54e-a23e199f96af, tags={ flutter_download_task, vn.hunghd.flutterdownloader.DownloadWorker } ]

上面的代碼有一些奇怪的地方。 首先請使用您的dir.path變量(不要對您的路徑進行硬編碼)。

然后 flutter 如果您不指定文件名,下載程序可以為該文件生成一個唯一的 id。 如果你想強制使用 name: fileName即使這個文件名不是唯一的也是有效的。 它會生成“相同”通知,您也可以打開該文件。

如果您檢查目錄,您會看到第二個文件名將是替換文件(文件名 (1))。

if(statuses[Permission.storage]!.isGranted){
          var dir = await getExternalStorageDirectory();
          if(dir != null) {
            final taskId = await FlutterDownloader.enqueue(
              url: instructionUrl,
              headers: {}, // optional: header send with url (auth token etc)
              savedDir: dir.path,
              /*fileName:"uniquename",*/
              showNotification: true, // show download progress in status bar (for Android)
              saveInPublicStorage: true,
              openFileFromNotification: true, // click on notification to open downloaded file (for Android)
            );
          }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM