Akhil B
Akhil B

Reputation: 69

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

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

Upvotes: 1

Views: 8127

Answers (2)

Sajjad
Sajjad

Reputation: 35

uses add permission

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

add this code

<application
    android:label="testavatarbetter"
    android:name="${applicationName}"
    android:icon="@mipmap/ic_launcher"
    android:requestLegacyExternalStorage="true">

enter image description here

Upvotes: 0

Khunt Arpit
Khunt Arpit

Reputation: 458

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

Upvotes: 4

Related Questions