Before we start:
1. External storage means a real sd card or a connected mass storage USB device.
2. It has nothing to do with File.DirRootExternal / DirDefaultExternal which actually point to an internal storage.
3. It has nothing to do with runtime permissions.
4. You can use RuntimePermissions.GetAllSafeDirsExternal to directly access a specific folder on the SD card.
5. The minimum version for this class is Android 5. It might work with Android 4.4 (change minSdkVersion if you like to try it).
Starting from Android 4.4 it is no longer possible to directly access external storages.
The only way to access these storages is through the Storage Access Framework (SAF), which is a quite complex and under-documented framework.
The ExternalStorage class makes it simpler to work with SAF.
1. Call ExternalStorage.SelectDir. This will open a dialog that will allow the user to select the root folder. Once selected the uri of the root folder is stored and can be later used without requiring the user to select the folder again. Even after the device is booted.
2. Wait For the ExternalFolderAvailable event.
Now you can access the files under Storage.Root, including inside subfolders.
3. Files are represented as a custom type named ExternalFile.
4. The following operations are supported: ListFiles, Delete, CreateNewFile, FindFile, OpenInputStream and OpenOutputStream.
See the attached example.
Depends on: ContentResolver and JavaObject libraries.
- V1.03: New CreateDir and FindOrCreateDir subs. Thank you @ac9ts and @DonManfred.
- V1.02: Fix for issue related to the conversion of URIs to strings.
- V1.01: Fixes an issue with Storage.ListFiles. Credit goes to @agraham !
12.4 KB Views: 567