Skip to content

Commit 6726d5e

Browse files
author
LaksCastro
committed
(#7) Add SAF API to README.md docs
1 parent b00757d commit 6726d5e

1 file changed

Lines changed: 57 additions & 8 deletions

File tree

README.md

Lines changed: 57 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ Plugin to fetch Android shared storage/folders info
99
- _**Android Only**_
1010
- _**Alpha version**_
1111
- _**Supports Android 4.1+ (API Level 16+)**_
12+
- _**The `targetSdk` should be set to `31`**_
1213

1314
### Features
1415

@@ -19,8 +20,8 @@ This plugin allow us to get path of top-level shared folder (Downloads, DCIM, Vi
1920
```dart
2021
/// Get Android [downloads] top-level shared folder
2122
/// You can also create a reference to a custom directory as: `EnvironmentDirectory.custom('Custom Folder')`
22-
final sharedDirectory =
23-
await getExternalStoragePublicDirectory(EnvironmentDirectory.downloads);
23+
final sharedDirectory =
24+
await getExternalStoragePublicDirectory(EnvironmentDirectory.downloads);
2425
2526
print(sharedDirectory.path); /// `/storage/emulated/0/Download`
2627
```
@@ -29,26 +30,74 @@ print(sharedDirectory.path); /// `/storage/emulated/0/Download`
2930

3031
```dart
3132
/// Get Android [downloads] shared folder for Android 9+
32-
final sharedDirectory =
33+
final sharedDirectory =
3334
await getMediaStoreContentDirectory(MediaStoreCollection.downloads);
3435
3536
print(sharedDirectory.path); /// `/external/downloads`
3637
```
3738

38-
- Get root Android path, note that is a read-only folder
39+
- 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
3940

4041
```dart
41-
/// Get Android root folder
42-
final sharedDirectory = await getRootDirectory();
42+
/// Get permissions to manage an Android directory
43+
final selectedUriDir = await openDocumentTree();
4344
44-
print(sharedDirectory.path); /// `/system`
45+
print(selectedUriDir);
46+
```
47+
48+
- Create a new file using the `SAF` API
49+
50+
```dart
51+
/// Create a new file using the `SAF` API
52+
final newDocumentFile = await createDocumentFile(
53+
mimeType: ' text/plain',
54+
content: 'My Plain Text Comment Created by shared_storage plugin',
55+
displayName: 'CreatedBySharedStorageFlutterPlugin',
56+
directory: anySelectedUriByTheOpenDocumentTreeAPI,
57+
);
58+
59+
print(newDocumentFile);
60+
```
61+
62+
- Get all persisted [URI]s by the `openDocumentTree` API, from `SAF` API
63+
64+
```dart
65+
/// You have [write] and [read] access to all persisted [URI]s
66+
final listOfPersistedUris = await persistedUriPermissions();
67+
68+
print(newDocumentFile);
69+
```
70+
71+
- Revoke a current persisted [URI], from `SAF` API
72+
73+
```dart
74+
/// Can be any [URI] returned by the `persistedUriPermissions`
75+
final uri = ...;
76+
77+
/// After calling this, you no longer has access to the [uri]
78+
await releasePersistableUriPermission(uri);
79+
80+
print(newDocumentFile);
81+
```
82+
83+
- 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)
84+
85+
```dart
86+
/// Can be any [URI], but the method will only return [true] if the [uri]
87+
/// is also present in the list returned by `persistedUriPermissions`
88+
final uri = ...;
89+
90+
/// Verify if you have [write] and [read] access to a given [uri]
91+
final isPersisted = await isPersistedUri(uri);
92+
93+
print(newDocumentFile);
4594
```
4695

4796
### Android API's
4897

4998
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:
5099

51-
[`🔗 android.os.Environment`](https://developer.android.com/reference/android/os/Environment#summary) [`🔗 android.provider.MediaStore`](https://developer.android.com/reference/android/provider/MediaStore#summary)
100+
[`🔗android.os.Environment`](https://developer.android.com/reference/android/os/Environment#summary) [`🔗android.provider.MediaStore`](https://developer.android.com/reference/android/provider/MediaStore#summary) [`🔗android.provider.DocumentsProvider`](https://developer.android.com/guide/topics/providers/document-provider)
52101

53102
<br>
54103

0 commit comments

Comments
 (0)