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 } ]
make sure you have a write permision. Not read, write permision
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
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>
//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"/>
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');
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.