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?
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.
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"
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
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With