Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Not able to access storage even if storage permission is granted in flutter

I’m using the permission_handler package to request permissions on Android. A user is able to download a file to the downloads folder when he clicks on "Allow" in the Permission.storage popup. (We are calling it Permission.storage when the user installs the app.)

The issue is that if a user clicks on "Don’t allow" the first time and "Allow" the second time in other flows in the app, we are still getting a permission denied error and the user is not able to download files like above flow. I’ve logged and checked the value of the status; it is coming as PermissionStatus.granted only which is expected. Any ideas on how to fix this?

Below is the error log

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I/flutter ( 8170): could not download file FileSystemException: Cannot open file, path = ‘/storage/emulated/0/Download/codes.txt’ (OS Error: Permission denied, errno = 13)

/// Permission snippet of first time
 final permission =
        GetPlatform.isAndroid ? Permission.storage : Permission.photos;
    final status = await permission.status;
    if (status != PermissionStatus.granted) {
      await permission.request().isGranted;
    }

/// Snippet when user clicks on download second time 
  final permission = Permission.storage;
        final status = await permission.status;
        debugPrint('>>>Status $status'); /// here it is coming as PermissionStatus.granted
        if (status != PermissionStatus.granted) {
          await permission.request().isGranted;
          debugPrint('>>> ${await permission.status}');
        }
        directory = Directory('/storage/emulated/0/Download');
       ///perform other stuff to download file

>Solution :

when permission_handler package send request to user with request() function, we made in await so after close dialog you check second time if permission granted or not and if permission granted then perform other stuff to download file else you can again send request for the permission.

/// Snippet when user clicks on download second time 
final permission = Permission.storage;
final status = await permission.status;
debugPrint('>>>Status $status'); /// here it is coming as PermissionStatus.granted
if (status != PermissionStatus.granted) {
  await permission.request();
  if(await permission.status.isGranted){
    directory = Directory('/storage/emulated/0/Download');
    ///perform other stuff to download file
  } else {
   await permission.request();
  }
  debugPrint('>>> ${await permission.status}');
}
directory = Directory('/storage/emulated/0/Download');
///perform other stuff to download file
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading