flutter: unable to download the media files using flutter_downloader package for android 13 plus versions

676 Views Asked by At

iam using,

  • flutter version:3.7.0
  • Dart version 2.19.0
  • android gradle version: 7.2.1
  • target sdk version is 33, android sdk version : 33
  • Java version Java(TM) SE Runtime Environment: 18.9

iam using flutter downloader version where below android 12 and below versions are working properly. when it comes to android 13 version is not working, but when i made prints it is showing status is complete even the file is not downloaded

Have added app/src/main/androidManifest.xml file:

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>

have also implemented device_info_plus plugin for identifying android sdk version and have requested the file permission for 13+ version:

Code:

Future<bool> _checkPermission() async {
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    if (androidInfo.version.sdkInt! >= 33) {
      isVideosPermission = await Permission.videos.status.isGranted;
      isPhotosPermission = await Permission.photos.status.isGranted;
    } else {
      isStoragePermission = await Permission.storage.status.isGranted;
    }
  if (isStoragePermission && isVideosPermission && isPhotosPermission) {
      PermissionStatus permission = await Permission.storage.status;
      if (permission != PermissionStatus.granted) {
        PermissionStatus permissionStatus = await Permission.storage.request();
        if (permissionStatus == PermissionStatus.granted) {
          return true;
        }
      } else {
        return true;
      }
    } else {
      return true;
    }
    return false;
  }

have successfully permission is added but unable to download the file, even the file downloading status is success (Complete).

what should i have to do?

Have upgraded the android sdk and target sdk version from 32 to 33. and added permissions in android manifest.xml and also imported the new library device_info_plus for detecting the android sdk version. and in the code at downloader.dart file have added few changes to android 13 version and above is different.

1

There are 1 best solutions below

10
Karishma Nadar On

Try Using this method

UPDATED

     Future<void> downloadFile() async {
    bool checkPermission1 = await Permission.storage.isGranted;

    //For android
    DeviceInfoPlugin plugin = DeviceInfoPlugin();
    AndroidDeviceInfo android = await plugin.androidInfo;
    if (android.version.sdkInt < 33) {
      if (await Permission.storage.request().isGranted) {
        setState(() {
          checkPermission1 = true;
        });
      } else if (await Permission.storage.request().isPermanentlyDenied) {
        await openAppSettings();
      } else if (await Permission.audio.request().isDenied) {
        setState(() {
          checkPermission1 = false;
        });
      }
    } else {
      if (await Permission.photos.request().isGranted) {
        setState(() {
          checkPermission1 = true;
        });
      } else if (await Permission.photos.request().isPermanentlyDenied) {
        await openAppSettings();
      } else if (await Permission.photos.request().isDenied) {
        setState(() {
          checkPermission1 = false;
        });
      }
    }

    print(checkPermission1);
    //for IOS
    if (checkPermission1 == false) {
      PermissionStatus status = await Permission.storage.request();
      print(status);
      checkPermission1 = await Permission.storage.isGranted;
    }

if (checkPermission1 == true) {
  String dirloc = "";
  if (Platform.isAndroid) {
    dirloc = "/sdcard/download/";
  } else {
    dirloc = (await getApplicationSupportDirectory()).path;
  }

  print('dirloc $dirloc');

  try {
    FileUtils.mkdir([dirloc]);
    await dio.download("your url", dirloc + widget.fileName!,
        onReceiveProgress: (receivedBytes, totalBytes) {
      setState(() {
        downloading = true;
        progress =
            ((receivedBytes / totalBytes) * 100).toStringAsFixed(0) + "%";
      });
    });
  } catch (e) {
    print(e);
  }

  setState(() {
    downloading = false;
    progress = "Download Completed.";
    path = dirloc + widget.fileName!;
  });
  Navigator.pop(context);
  if (path != "No Data") await Share.shareFiles([path]);
  print("path $path");
} else {
  setState(() {
    progress = "Permission Denied!";
    Navigator.pop(context);
  });
   }
  }

Updating with the permissions i have added

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_MEDIA_STORAGE"/>
<uses-permission android:name="android.permission.READ_MEDIA_STORAGE"/>
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES"/>
<uses-permission android:name="android.permission.READ_MEDIA_VIDEO"/>
<uses-permission android:name="android.permission.READ_MEDIA_AUDIO"/>