Plugin to fetch Android shared storage/folders info
- Android Only
- Alpha version
- Supports Android 4.1+ (API Level 16+)
- The
targetSdkshould be set to31
- Get top-level external/shared folders path from
EnvironmentAndroid API
This plugin allow us to get path of top-level shared folder (Downloads, DCIM, Videos, Audio) using the following Android API's
/// Get Android [downloads] top-level shared folder
/// You can also create a reference to a custom directory as: `EnvironmentDirectory.custom('Custom Folder')`
final sharedDirectory =
await getExternalStoragePublicDirectory(EnvironmentDirectory.downloads);
print(sharedDirectory.path); /// `/storage/emulated/0/Download`- Get external/shared folders path from
MediaStoreAndroid API
/// Get Android [downloads] shared folder for Android 9+
final sharedDirectory =
await getMediaStoreContentDirectory(MediaStoreCollection.downloads);
print(sharedDirectory.path); /// `/external/downloads`- Start
OPEN_DOCUMENT_TREEactivity to prompt user to select an folder to enable write and read access to be used by theStorage Access FrameworkAPI
/// Get permissions to manage an Android directory
final selectedUriDir = await openDocumentTree();
print(selectedUriDir);- Create a new file using the
SAFAPI
/// Create a new file using the `SAF` API
final newDocumentFile = await createDocumentFile(
mimeType: ' text/plain',
content: 'My Plain Text Comment Created by shared_storage plugin',
displayName: 'CreatedBySharedStorageFlutterPlugin',
directory: anySelectedUriByTheOpenDocumentTreeAPI,
);
print(newDocumentFile);- Get all persisted [URI]s by the
openDocumentTreeAPI, fromSAFAPI
/// You have [write] and [read] access to all persisted [URI]s
final listOfPersistedUris = await persistedUriPermissions();
print(listOfPersistedUris);- Revoke a current persisted [URI], from
SAFAPI
/// Can be any [URI] returned by the `persistedUriPermissions`
final uri = ...;
/// After calling this, you no longer has access to the [uri]
await releasePersistableUriPermission(uri);- Convenient method to know if a given [uri] is a persisted
uri("persisted uri" means that you havewriteandreadaccess to theurieven if devices reboot)
/// Can be any [URI], but the method will only return [true] if the [uri]
/// is also present in the list returned by `persistedUriPermissions`
final uri = ...;
/// Verify if you have [write] and [read] access to a given [uri]
final isPersisted = await isPersistedUri(uri);Most Flutter plugins uses Android API's under the hood. So this plugin do the same, and to retrieve Android shared folder paths the following API's are being used:
🔗android.os.Environment 🔗android.provider.MediaStore 🔗android.provider.DocumentsProvider
Copyright © 2021-present, Laks Castro.
Shared Storage is MIT licensed 💖