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

Time:04-01

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

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

CodePudding user response:

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
  • Related