最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

android - Flutter WebView File Download Not Working - Stack Overflow

programmeradmin5浏览0评论

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:

  1. Checked Android 13+ permission changesWRITE_EXTERNAL_STORAGE is deprecated, so I added MANAGE_EXTERNAL_STORAGE.
  2. Verified requestLegacyExternalStorage in AndroidManifest.xml – This should help with compatibility for older devices.
  3. Tried different directories – getExternalStorageDirectory() and /storage/emulated/0/Download, but no success.
  4. 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?

发布评论

评论列表(0)

  1. 暂无评论