7

I tried to download pdf files from firebase storage using flutter downloader package. But download always shows failed.I dont know what is the reason.

This is the code I tried. Please suggest some solutions.Thanks in advance.

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);
  }
}

This is the error I received .

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
  • did you give external storage write permission? Commented Feb 11, 2019 at 9:35
  • what is the error message that is printed to console? Commented Feb 11, 2019 at 10:24
  • yeah I gave the permission to external storage This is the error message. Commented Feb 13, 2019 at 6:17

5 Answers 5

1

make sure your file name is different because same file name not allow to store

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

    if (status.isGranted) {
     

      final id = await FlutterDownloader.enqueue(
        url: url,
        savedDir: "/storage/emulated/0/Download",
        showNotification: true,
        openFileFromNotification: true,
  fileName: DateTime.now().millisecond.toString().replaceAll(" ", ""),

      ).then((value) {
        print("complated");
      });
    } else {
      setState(() {
       print("faild");
      });
      print('Permission Denied');
    }
  }
Sign up to request clarification or add additional context in comments.

Comments

0

make sure you have a write permision. Not read, write permision

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

Comments

0

Try adding android:requestLegacyExternalStorage="true" to the flutter_downloader provider in android/app/src/main/AndroidManifest.xml like:

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

1 Comment

May I know why do we need to add this flag?
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"/>

Comments

0

You can use this method to easily download any material by providing a URL.

downloadStartMethodUsingFlutterDownloader(controller, uri) async {
print("URI: $uri");
final taskId = await FlutterDownloader.enqueue(
  url: uri.url.toString(),
  savedDir: (await getExternalStorageDirectory())!.path,

  // allowCellular: true,
  fileName: uri.url.pathSegments.last.toString(),
  showNotification:
      true, // show download progress in status bar (for Android)
  openFileFromNotification:
      true, // click on notification to open downloaded file (for Android)
  allowCellular: true,
);

}

You can call it as:

Widget build(BuildContext context) {
return Scaffold(
  appBar: AppBar(
    title: Text('Testing the downloading'),
  ),
  body: InAppWebView(
    initialOptions: InAppWebViewGroupOptions(
      crossPlatform: InAppWebViewOptions(useOnDownloadStart: true),
    ),
    initialUrlRequest: URLRequest(url: Your_URL_HERE),

    // new from stackoverflow
    onDownloadStartRequest: downloadStartMethodUsingFlutterDownloader,
  ),
);

}

These are the imports that are required:

import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_inappwebview/flutter_inappwebview.dart';

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.