Reputation: 750
Is there a plugin for this problem or can it be solved using dart:io
, because path_provider
does not have a method for accessing the root directory?
Upvotes: 13
Views: 18869
Reputation: 53
Package external_path returns the path for top level storage.
First install the plugin:
flutter pub add external_path
Then import it:
import 'package:external_path/external_path.dart';
Finally run something like this:
var path = await ExternalPath.getExternalStorageDirectories(); // [/storage/emulated/0, /storage/B3AE-4D28] 2nd path refers to SD
Check out more here in the docs: https://pub.dev/documentation/external_path/latest/
Upvotes: 0
Reputation: 171
when we take path by:
/data/user/0/com.crackhead.stacker/app_flutter/ and
/storage/emulated/0/Android/data/com.crackhead.stacker/files
soo here from 2nd one we see that internal storage path would be
/storage/emulated/0/
and for SD-card I had to hard-code seeing from Z-archiver app where path was:
/storage/4DC2-723F
and there corresponding directories would be:
Internal Storage : Directory('/storage/emulated/0/');
SD-card : Directory('/storage/4DC2-723F');
Upvotes: 3
Reputation: 652
use this package https://pub.dev/packages/external_path to get root Directory of the device.
Future<void> getPath_1() async {
var path = await ExternalPath.getExternalStorageDirectories();
print(path); // [/storage/emulated/0, /storage/B3AE-4D28]
}
Upvotes: 2
Reputation: 11496
You can use this utility function:
import 'dart:io';
Directory findRoot(FileSystemEntity entity) {
final Directory parent = entity.parent;
if (parent.path == entity.path) return parent;
return findRoot(parent);
}
If you have a package such as path_provider
, you can use it like this in your code:
import 'package:path_provider/path_provider.dart';
final Directory root = findRoot(await getApplicationDocumentsDirectory());
Upvotes: 2
Reputation: 388
The Directory class may have what you are looking for.
var myDir = new Directory('path_to_root');
Upvotes: -2