public class VolumeManager extends Object
The purpose of a volume manager is to access the external Storage Units of an Android device,
as any other part of the file system. For each plugged-in unit, you get
a Volume
object.
When developing on a device without any USB connector or with a fewer number
of connectors than the expected target, you can fake one or more existing ordinary internal directory
as an additional Storage Container hosting emulated USB devices,
with addEmulatedStorageContainer()
.
In that directory, normal files are ignored but each directory simulates a mount point for a volume.
Examples
Here is a possible tree for one emulated Storage Container having two USB devices:
/mnt/sdcard/Movies/ |_ usb-emu1/ | |_ .libmedia | |_ myVideo.mp4 |_ usb-emu2/ |_ myOtherVideo.mp4
Here is a partial sample of usage in an application:
VolumeManager volumeManager = new VolumeManager(); volumeManager.addEmulatedStorageContainer("/mnt/sdcard/Movies"); List<Volume> volumes = volumeManager.getVolumes(); for (Volume v: volumes) { Log.d(TAG, "volume label: " + v.getLabel()); }Suppose there is a USB stick labeled
Android
and that the .libmedia
file contains MyLabel
,
here is the potential output:
volume label: Android volume label: MyLabel volume label: usb-emu2
Constructor and Description |
---|
VolumeManager()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
boolean |
addEmulatedStorageContainer(File directory)
Adds an emulated storage container where looking for volumes.
|
boolean |
addEmulatedStorageContainer(String pathname)
Adds an emulated storage container where looking for volumes.
|
String[] |
getDebugReport()
Builds a debug report.
|
static Intent |
getStorageAccessIntent()
Returns an intent to spin up the Storage Access Framework picker UI and select a directory subtree.
|
List<Volume> |
getVolumes()
Returns the list of currently available volumes.
|
void |
logDebugReport()
Writes the lines from
getDebugReport() to the log. |
static void |
onStorageAccessResult(Intent resultData)
Persists the authorization to access the tree selected by the Storage Access Framework picker UI.
|
static void |
postponeStorageAccessFrameworkUntilMarshmallow()
Postpones the usage of Storage Access Framework until Marshmallow (6.0) version.
|
public static void postponeStorageAccessFrameworkUntilMarshmallow()
Staying with the default is recommended.
public static Intent getStorageAccessIntent()
postponeStorageAccessFrameworkUntilMarshmallow()
null
.public static void onStorageAccessResult(Intent resultData)
getStorageAccessIntent()
and when resultCode is RESULT_OK.resultData
- What is returned by onActivityResult().public boolean addEmulatedStorageContainer(String pathname)
pathname
- The pathname to a directory hosting emulated volumes.true
if the pathname designates an existing entry and this entry is a directory.public boolean addEmulatedStorageContainer(File directory)
directory
- The directory hosting emulated volumes.true
if the argument designates an existing entry and this entry is a directory.public List<Volume> getVolumes()
public void logDebugReport()
getDebugReport()
to the log. Logging is done with the Debug level.public String[] getDebugReport()