Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Permission denied at ExternalStorageDirectory access via Flutter plugin

I wrote an plugin to access the ExternalStorageDirectory on Android via Dart.

android specific code for the plugin:

@Override
  public void onMethodCall(MethodCall call, Result result) {
    if (call.method.equals("getUserDataDirectory")) {
      String path = Environment.getExternalStorageDirectory().getAbsolutePath();
      result.success(path);
    } else {
      result.notImplemented();
    }
  }

The returned path is correct storage/emulated/0. But now if i try to iterate throw the directory i get an Permission denied.

error.log

[ERROR:topaz/lib/tonic/logging/dart_error.cc(16)] Unhandled exception:
E/flutter ( 3381): FileSystemException: Directory listing failed, path = '/storage/emulated/0/' (OS Error: Permission denied, errno = 13)
E/flutter ( 3381): #0      _Directory._fillWithDirectoryListing (dart:io-patch/directory_patch.dart:32)
E/flutter ( 3381): #1      _Directory.listSync (directory_impl.dart:214)
E/flutter ( 3381): #2      _MyAppState.initPathRequest (/data/user/0/com.yourcompany.userdatadirectoryexample/cache/exampleIAKNFP/example/lib/main.dart:34:25)

main.dart

  path = await Userdatadirectory.getUserDataDirectory;

    var dir = new Directory(path);

    List contents = dir.listSync();
    for (var fileOrDir in contents) {
      print(fileOrDir.path);
    }

my example/android/app/src/AndroidManifest.xml contains these additional premissions:

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

What is the problem here?

like image 373
Lukas Kirner Avatar asked Oct 11 '17 22:10

Lukas Kirner


People also ask

How do you get permission in flutter?

If a user has already denied, only thing could be done is to redirect the user to application settings to enable needed permissions. isPermanentlyDenied is returned only as result of request(). If just check for permission status - it says Denied.


4 Answers

it'll happen if you're on Android Q | 10.

And it will be solved by adding the following on the manifest file.

<application
        android:requestLegacyExternalStorage="true"
like image 142
TejaDroid Avatar answered Oct 11 '22 05:10

TejaDroid


use permission_handler

import 'package:permission_handler/permission_handler.dart';

before download action you need check storage permission:

var status = await Permission.storage.status;
if (!status.isGranted) {
   await Permission.storage.request();
}

hope to help some body

like image 25
Hưng Trịnh Avatar answered Oct 11 '22 05:10

Hưng Trịnh


You can use either of these 2 package to ask for device permission: https://pub.dartlang.org/packages/simple_permissions or https://pub.dartlang.org/packages/permission . They have simple APIs and comprehensive examples.

like image 2
TruongSinh Avatar answered Oct 11 '22 04:10

TruongSinh


Add the following on AndroidManifest.xml :

<application
        android:requestLegacyExternalStorage="true"

Also make these changes in android\app\build.gradle

compileSdkVersion 29

targetSdkVersion 29

It will work.

like image 1
Aqeel Avatar answered Oct 11 '22 06:10

Aqeel