daentech / shared-storage

📁 Flutter plugin to get Android shared folders like DCIM, Downloads, Video, Audio. Works with Android 4.1+ (API Level 16+)

Home Page:https://pub.dev/packages/shared_storage

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Shared Storage Flutter Plugin

pub package

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 to 31

Features

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 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 the Storage 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, from SAF 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 have write and read access to the uri 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 💖

About

📁 Flutter plugin to get Android shared folders like DCIM, Downloads, Video, Audio. Works with Android 4.1+ (API Level 16+)

https://pub.dev/packages/shared_storage

License:MIT License


Languages

Language:Dart 55.0%Language:Kotlin 45.0%