shared_storage 0.2.0 shared_storage: ^0.2.0 copied to clipboard
๐ Flutter plugin to get Android shared folders like DCIM, Downloads, Video, Audio. Works with Android 4.1 (API Level 16+)
Shared Storage Flutter Plugin #
Plugin to fetch Android shared storage/folders info
Notes #
- Android Only
- Alpha version
- Supports Android 4.1+ (API Level 16+)
- The
targetSdk
should be set to31
Features #
- Get top-level external/shared folders path from
Environment
Android 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
MediaStore
Android API
/// Get Android [downloads] shared folder for Android 9+
final sharedDirectory =
await getMediaStoreContentDirectory(MediaStoreCollection.downloads);
print(sharedDirectory.path); /// `/external/downloads`
- Start
OPEN_DOCUMENT_TREE
activity to prompt user to select an folder to enable write and read access to be used by theStorage Access Framework
API
/// Get permissions to manage an Android directory
final selectedUriDir = await openDocumentTree();
print(selectedUriDir);
- Create a new file using the
SAF
API
/// 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
openDocumentTree
API, fromSAF
API
/// You have [write] and [read] access to all persisted [URI]s
final listOfPersistedUris = await persistedUriPermissions();
print(listOfPersistedUris);
- Revoke a current persisted [URI], from
SAF
API
/// 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 havewrite
andread
access to theuri
even 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);
Android API's #
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
Open Source
Copyright ยฉ 2021-present, Laks Castro.
Shared Storage is MIT licensed ๐