Reputation: 4319
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?
Upvotes: 8
Views: 7898
Reputation: 1037
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
Upvotes: 3
Reputation: 971
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.
Upvotes: 1
Reputation: 6811
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"
Upvotes: 5
Reputation: 4856
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.
Upvotes: 2