8

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?

Lukas Kirner
  • 3,989
  • 6
  • 23
  • 30
  • 1
    Check for Runtime Permission. Refer https://developer.android.com/guide/topics/permissions/requesting.html#normal-dangerous – Pritesh Patel Oct 11 '17 at 23:06
  • Possible Duplicate https://stackoverflow.com/questions/31162638/grant-permission-required-for-external-storage-in-android-m – miversen33 Oct 11 '17 at 23:48
  • Ok but now i have te problem that i cant acces the Activity so i cant call requestPermission(). So is it possible to get the Context or Activity in an Flutter plugin project? And how? – Lukas Kirner Oct 12 '17 at 12:11
  • 1
    I am trying to access /storage/emulated/ directory but getting error "Unhandled Exception: FileSystemException: Directory listing failed, path = '/storage/emulated/' (OS Error: Permission denied, errno = 13)" while i have granted WRITE_EXTERNAL_STORAGE and READ_EXTERNAL_STORAGE permission. Any suggestion. Thanks. – Kamlesh Oct 27 '20 at 09:03

4 Answers4

5

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"
TejaDroid
  • 6,561
  • 4
  • 31
  • 38
3

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

Hưng Trịnh
  • 947
  • 1
  • 12
  • 23
2

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.

TruongSinh
  • 4,662
  • 32
  • 52
1

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.

Aqeel
  • 804
  • 1
  • 11
  • 15