In my Flutter application, I have to download multiple files and have to store it in application storage.
For that I am using Dio and added below plugin dependencies in pubspec.yaml file :
dio: any
path_provider: any
In main.dart, I have below variables :
String uri =
'https://i.picsum.photos/id/698/200/200.jpg?hmac=EElVlYPe8BAq1Btf4bWUxP9NoQP01_e8LTUzpbdKbgY';
bool downloading = false;
where uri is the URL for an Image online.
Inside initState() method, called the downloadFile() method, which is as below :
Future<void> downloadFile() async{
Dio dio= Dio();
try{
var dir= await getApplicationDocumentsDirectory();
await dio.download(uri, "${dir.path}/myimg.jpg", onReceiveProgress: (rec,total){
setState(() {
downloading=true;
});
});
}catch(e)
{
print("Error >> "+e.toString());
}
setState(() {
downloading=false;
print("Download Completes");
});
}
But, I am getting below error :
HTTP Status Error 403
MSG_WINDOW_FOCUS_CHANGED 1 1 D/InputMethodManager(15750): startInputInner - Id : 0 I/InputMethodManager(15750): startInputInner - mService.startInputOrWindowGainedFocus D/InputMethodManager(15750): startInputInner - Id : 0 I/flutter (15750): DioError [DioErrorType.response]: Http status error [403] I/flutter (15750): Source stack: I/flutter (15750): #0 DioMixin.fetch (package:dio/src/dio_mixin.dart:488:35) I/flutter (15750): #1 DioMixin.request (package:dio/src/dio_mixin.dart:483:12) I/flutter (15750): #2 DioForNative.download (package:dio/src/entry/dio_for_native.dart:84:24) I/flutter (15750): #3 _MyHomePageState.downloadFile (package:downloadfiles/main.dart:82:17) I/flutter (15750): I/flutter (15750): Download Completes
I am getting this error while running the app in android device, I have manually given File storage permission from settings for the application. Also updated AndroidManifest.xml file as below :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.INTERNET"/>
Still am getting above error. What might causing the issue?