I have a Flutter app that uses flutter_inappwebview
to display web content, and I want to enable file downloads (especially PDFs) inside the WebView. I am using flutter_downloader
for this purpose.
Problem
On Android 13+ devices, the file download does not start, and no permission request is shown. Instead, I get a permission error. However, it works fine on devices running Android 12 or lower.
My Implementation
Permissions in AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.ACCESS_MEDIA_LOCATION"/>
<application android:requestLegacyExternalStorage="true">
Permission Request in Flutter (Dart Code)
Future<void> _checkAndRequestPermissions() async {
if (Platform.isAndroid) {
Map<Permission, PermissionStatus> statuses = await [
Permission.storage,
Permission.manageExternalStorage,
].request();
if (statuses[Permission.storage] != PermissionStatus.granted) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Storage permission is required!")),
);
}
}
}
Download Implementation
Future<void> _downloadFile(String url) async {
if (await Permission.storage.request().isGranted) {
final directory = Directory('/storage/emulated/0/Download');
final taskId = await FlutterDownloader.enqueue(
url: url,
savedDir: directory.path,
showNotification: true,
openFileFromNotification: true,
);
if (taskId != null) {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Downloading...")),
);
}
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text("Storage permission is required!")),
);
}
}
Expected Behavior:
- On Android 12 and below, the app asks for permission, and downloads work fine.
- On Android 13+, no permission prompt appears, and the file does not download.
What I’ve Tried:
- Checked Android 13+ permission changes –
WRITE_EXTERNAL_STORAGE
is deprecated, so I addedMANAGE_EXTERNAL_STORAGE
. - Verified
requestLegacyExternalStorage
inAndroidManifest.xml
– This should help with compatibility for older devices. - Tried different directories –
getExternalStorageDirectory()
and/storage/emulated/0/Download
, but no success. - Tested
openAppSettings()
when permission is denied – It redirects to settings, but the permission toggle does not appear.
Question:
- How can I properly request storage permissions for file downloads on Android 13+ and make
flutter_inappwebview
allow downloads? - Would switching to the Media Store API be necessary, or is there a workaround with
flutter_downloader
?