fluttercommunity / flutter_downloader

Flutter Downloader - A plugin for creating and managing download tasks.
https://pub.dev/packages/flutter_downloader
BSD 3-Clause "New" or "Revised" License
920 stars 517 forks source link

open failed: EACCES (Permission denied) when retry download after paused on Android 10 (Virtual and physical device) #622

Open vietlinhtspt opened 2 years ago

vietlinhtspt commented 2 years ago

I am running the example code on android 10 (physical device), it is working properly when starting, but after I paused it, I cannot re-download any way. Then I tested with android 9, 10, 11, 12 by virtual devices, and found out that this issue was only caused on android 10.

Step to re-procedure:

  1. Clone plugin's example code.
  2. flutter pub get
  3. flutter run (using virtual or physical device run android 10)

Debug console:

D/DownloadWorker(15093): DownloadWorker{url=http://barbra-coco.dyndns.org/student/learning_android_studio.pdf,filename=learning_android_studio.pdf,savedDir=/storage/emulated/0/Download,header={"auth": "test_for_sql_encoding"},isResume=false,status=1 D/DownloadWorker(15093): Update notification: {notificationId: 1, title: learning_android_studio.pdf, status: 2, progress: 0} I/flutter (15093): Background Isolate Callback: task (c0e7b302-49ee-4540-a088-e0fdb73ed0d6) is in status (DownloadTaskStatus(2)) and process (0) I/flutter (15093): UI Isolate Callback: [c0e7b302-49ee-4540-a088-e0fdb73ed0d6, DownloadTaskStatus(2), 0] D/DownloadWorker(15093): exists file for learning_android_studio.pdfautomatic resuming... D/DownloadWorker(15093): Open connection to http://barbra-coco.dyndns.org/student/learning_android_studio.pdf D/DownloadWorker(15093): Headers = {"auth": "test_for_sql_encoding"} D/DownloadWorker(15093): Resume download: Range: bytes=2967060- D/DownloadWorker(15093): Content-Type = application/pdf D/DownloadWorker(15093): Content-Length = 22237883 D/DownloadWorker(15093): Charset = null D/DownloadWorker(15093): fileName = learning_android_studio.pdf D/DownloadWorker(15093): Update too frequently!!!!, but it is the final update, we should sleep a second to ensure the update call can be processed I/flutter (15093): Background Isolate Callback: task (c0e7b302-49ee-4540-a088-e0fdb73ed0d6) is in status (DownloadTaskStatus(4)) and process (-1) I/flutter (15093): UI Isolate Callback: [c0e7b302-49ee-4540-a088-e0fdb73ed0d6, DownloadTaskStatus(4), -1] D/DownloadWorker(15093): Update notification: {notificationId: 1, title: learning_android_studio.pdf, status: 4, progress: -1} W/System.err(15093): java.io.FileNotFoundException: /storage/emulated/0/Download/learning_android_studio.pdf: open failed: EACCES (Permission denied) W/System.err(15093): at libcore.io.IoBridge.open(IoBridge.java:496) W/System.err(15093): at java.io.FileOutputStream.(FileOutputStream.java:235) W/System.err(15093): at java.io.FileOutputStream.(FileOutputStream.java:157) W/System.err(15093): at vn.hunghd.flutterdownloader.DownloadWorker.downloadFile(DownloadWorker.java:372) W/System.err(15093): at vn.hunghd.flutterdownloader.DownloadWorker.doWork(DownloadWorker.java:222) W/System.err(15093): at androidx.work.Worker$1.run(Worker.java:86) W/System.err(15093): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) W/System.err(15093): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) W/System.err(15093): at java.lang.Thread.run(Thread.java:919) W/System.err(15093): Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied) W/System.err(15093): at libcore.io.Linux.open(Native Method) W/System.err(15093): at libcore.io.ForwardingOs.open(ForwardingOs.java:167) W/System.err(15093): at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252) W/System.err(15093): at libcore.io.ForwardingOs.open(ForwardingOs.java:167) W/System.err(15093): at android.app.ActivityThread$AndroidOs.open(ActivityThread.java:7255) W/System.err(15093): at libcore.io.IoBridge.open(IoBridge.java:482) W/System.err(15093): ... 8 more I/WM-WorkerWrapper(15093): Worker result SUCCESS for Work [ id=c0e7b302-49ee-4540-a088-e0fdb73ed0d6, tags={ flutter_download_task, vn.hunghd.flutterdownloader.DownloadWorker } ]

UPDATE:

I have found that I can retry after removed the downloaded file (Maybe a part of).

IlyaMax commented 2 years ago

I resolved this by adding final status = await Permission.storage.request();

raaja-guidely commented 1 year ago

@vietlinhtspt did you fixed this?

stepushchik commented 1 week ago

It looks like the problem is using FileOutputStream for Download directory for Android 10.

https://github.com/fluttercommunity/flutter_downloader/blob/v1.11.8/android/src/main/kotlin/vn/hunghd/flutterdownloader/DownloadWorker.kt#L379

if (isResume) {
    // 1. continue downloading (append data to partial downloaded file)
    savedFilePath = savedDir + File.separator + actualFilename
    outputStream = FileOutputStream(savedFilePath, true)
}