StorageVolume | API reference | Android Developers (original) (raw)
public final class StorageVolumeextends [Object](/reference/java/lang/Object) implements[Parcelable](/reference/android/os/Parcelable)
Information about a shared/external storage volume for a specific user.
A device always has one (and one only) primary storage volume, but it could have extra volumes, like SD cards and USB drives. This object represents the logical view of a storage volume for a specific user: different users might have different views for the same physical volume (for example, if the volume is a built-in emulated storage).
The storage volume is not necessarily mounted, applications should use [getState()](/reference/android/os/storage/StorageVolume#getState%28%29) to verify its state.
Applications willing to read or write to this storage volume needs to get a permission from the user first, which can be achieved in the following ways:
- To get access to standard directories (like the
[Environment.DIRECTORY_PICTURES](/reference/android/os/Environment#DIRECTORY%5FPICTURES)), they can use the[createAccessIntent(String)](/reference/android/os/storage/StorageVolume#createAccessIntent%28java.lang.String%29). This is the recommend way, since it provides a simpler API and narrows the access to the given directory (and its descendants). - To get access to any directory (and its descendants), they can use the Storage Access Framework APIs (such as
[Intent.ACTION_OPEN_DOCUMENT](/reference/android/content/Intent#ACTION%5FOPEN%5FDOCUMENT)and[Intent.ACTION_OPEN_DOCUMENT_TREE](/reference/android/content/Intent#ACTION%5FOPEN%5FDOCUMENT%5FTREE), although these APIs do not guarantee the user will select this specific volume. - To get read and write access to the primary storage volume, applications can declare the
[Manifest.permission.READ_EXTERNAL_STORAGE](/reference/android/Manifest.permission#READ%5FEXTERNAL%5FSTORAGE)and[Manifest.permission.WRITE_EXTERNAL_STORAGE](/reference/android/Manifest.permission#WRITE%5FEXTERNAL%5FSTORAGE)permissions respectively, with the latter including the former. This approach is discouraged, since users may be hesitant to grant broad access to all files contained on a storage device.
It can be obtained through [StorageManager.getStorageVolumes()](/reference/android/os/storage/StorageManager#getStorageVolumes%28%29) and[StorageManager.getPrimaryStorageVolume()](/reference/android/os/storage/StorageManager#getPrimaryStorageVolume%28%29) and also as an extra in some broadcasts (see [EXTRA_STORAGE_VOLUME](/reference/android/os/storage/StorageVolume#EXTRA%5FSTORAGE%5FVOLUME)).
See [Environment.getExternalStorageDirectory()](/reference/android/os/Environment#getExternalStorageDirectory%28%29) for more info about shared/external storage semantics.
Summary
| Constants | |
|---|---|
| String | EXTRA_STORAGE_VOLUME Name of the Parcelable extra in the Intent.ACTION_MEDIA_REMOVED,Intent.ACTION_MEDIA_UNMOUNTED, Intent.ACTION_MEDIA_CHECKING,Intent.ACTION_MEDIA_NOFS, Intent.ACTION_MEDIA_MOUNTED,Intent.ACTION_MEDIA_SHARED, Intent.ACTION_MEDIA_BAD_REMOVAL,Intent.ACTION_MEDIA_UNMOUNTABLE, and Intent.ACTION_MEDIA_EJECT broadcast that contains a StorageVolume. |
| Inherited constants |
|---|
| From interfaceandroid.os.Parcelable int CONTENTS_FILE_DESCRIPTOR Descriptor bit used with describeContents(): indicates that the Parcelable object's flattened representation includes a file descriptor. int PARCELABLE_WRITE_RETURN_VALUE Flag for use with writeToParcel(Parcel, int): the object being written is a return value, that is the result of a function such as "Parcelable someFunction()", "void someFunction(out Parcelable)", or "void someFunction(inout Parcelable)". |
| Fields | |
|---|---|
| public static finalCreator<StorageVolume> | CREATOR |
| Public methods | |
|---|---|
| Intent | createAccessIntent(String directoryName) This method was deprecated in API level 29. Callers should migrate to using Intent.ACTION_OPEN_DOCUMENT_TREE instead. Launching this Intent on devices runningBuild.VERSION_CODES.Q or higher, will immediately finish with a result code of Activity.RESULT_CANCELED. |
| Intent | createOpenDocumentTreeIntent() Builds an Intent.ACTION_OPEN_DOCUMENT_TREE to allow the user to grant access to any directory subtree (or entire volume) from the DocumentsProviders available on the device. |
| int | describeContents() Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. |
| boolean | equals(Object obj) Indicates whether some other object is "equal to" this one. |
| String | getDescription(Context context) Returns a user-visible description of the volume. |
| File | getDirectory() Returns the directory where this volume is currently mounted. |
| String | getMediaStoreVolumeName() Return the volume name that can be used to interact with this storage device through MediaStore. |
| UserHandle | getOwner() Returns the user that owns this volume |
| String | getState() Returns the current state of the volume. |
| UUID | getStorageUuid() Gets the converted volume UUID. |
| String | getUuid() Gets the volume UUID, if any. |
| int | hashCode() Returns a hash code value for the object. |
| boolean | isEmulated() Returns true if the volume is emulated. |
| boolean | isPrimary() Returns true if the volume is the primary shared/external storage, which is the volume backed by Environment.getExternalStorageDirectory(). |
| boolean | isRemovable() Returns true if the volume is removable. |
| String | toString() Returns a string representation of the object. |
| void | writeToParcel(Parcel parcel, int flags) Flatten this object in to a Parcel. |
| Inherited methods |
|---|
| From class java.lang.Object Object clone() Creates and returns a copy of this object. boolean equals(Object obj) Indicates whether some other object is "equal to" this one. void finalize() Called by the garbage collector on an object when garbage collection determines that there are no more references to the object. finalClass<?> getClass() Returns the runtime class of this Object. int hashCode() Returns a hash code value for the object. final void notify() Wakes up a single thread that is waiting on this object's monitor. final void notifyAll() Wakes up all threads that are waiting on this object's monitor. String toString() Returns a string representation of the object. final void wait(long timeoutMillis, int nanos) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. final void wait(long timeoutMillis) Causes the current thread to wait until it is awakened, typically by being notified or interrupted, or until a certain amount of real time has elapsed. final void wait() Causes the current thread to wait until it is awakened, typically by being notified or interrupted. |
| From interface android.os.Parcelable abstract int describeContents() Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. abstract void writeToParcel(Parcel dest, int flags) Flatten this object in to a Parcel. |
Constants
Fields
Public methods
createAccessIntent
public Intent createAccessIntent (String directoryName)
This method was deprecated in API level 29.
Callers should migrate to using [Intent.ACTION_OPEN_DOCUMENT_TREE](/reference/android/content/Intent#ACTION%5FOPEN%5FDOCUMENT%5FTREE) instead. Launching this [Intent](/reference/android/content/Intent) on devices running[Build.VERSION_CODES.Q](/reference/android/os/Build.VERSION%5FCODES#Q) or higher, will immediately finish with a result code of [Activity.RESULT_CANCELED](/reference/android/app/Activity#RESULT%5FCANCELED).
Builds an intent to give access to a standard storage directory or entire volume after obtaining the user's approval.
When invoked, the system will ask the user to grant access to the requested directory (and its descendants). The result of the request will be returned to the activity through theonActivityResult method.
To gain access to descendants (child, grandchild, etc) documents, use[DocumentsContract.buildDocumentUriUsingTree(Uri,String)](/reference/android/provider/DocumentsContract#buildDocumentUriUsingTree%28android.net.Uri,%20java.lang.String%29), or[DocumentsContract.buildChildDocumentsUriUsingTree(Uri,String)](/reference/android/provider/DocumentsContract#buildChildDocumentsUriUsingTree%28android.net.Uri,%20java.lang.String%29) with the returned URI.
If your application only needs to store internal data, consider using[Context.getExternalFilesDirs](/reference/android/content/Context#getExternalFilesDirs%28java.lang.String%29),[Context.getExternalCacheDirs()](/reference/android/content/Context#getExternalCacheDirs%28%29), or [Context.getExternalMediaDirs()](/reference/android/content/Context#getExternalMediaDirs%28%29), which require no permissions to read or write.
Access to the entire volume is only available for non-primary volumes (for the primary volume, apps can use the [Manifest.permission.READ_EXTERNAL_STORAGE](/reference/android/Manifest.permission#READ%5FEXTERNAL%5FSTORAGE) and[Manifest.permission.WRITE_EXTERNAL_STORAGE](/reference/android/Manifest.permission#WRITE%5FEXTERNAL%5FSTORAGE) permissions) and should be used with caution, since users are more likely to deny access when asked for entire volume access rather than specific directories.
| Parameters | |
|---|---|
| directoryName | String: must be one of Environment.DIRECTORY_MUSIC,Environment.DIRECTORY_PODCASTS, Environment.DIRECTORY_RINGTONES,Environment.DIRECTORY_ALARMS, Environment.DIRECTORY_NOTIFICATIONS,Environment.DIRECTORY_PICTURES, Environment.DIRECTORY_MOVIES,Environment.DIRECTORY_DOWNLOADS, Environment.DIRECTORY_DCIM, orEnvironment.DIRECTORY_DOCUMENTS, or null to request access to the entire volume. |
| Returns | |
|---|---|
| Intent | intent to request access, or null if the requested directory is invalid for that volume. |
createOpenDocumentTreeIntent
public Intent createOpenDocumentTreeIntent ()
Builds an [Intent.ACTION_OPEN_DOCUMENT_TREE](/reference/android/content/Intent#ACTION%5FOPEN%5FDOCUMENT%5FTREE) to allow the user to grant access to any directory subtree (or entire volume) from the [DocumentsProvider](/reference/android/provider/DocumentsProvider)s available on the device. The initial location of the document navigation will be the root of this [StorageVolume](/reference/android/os/storage/StorageVolume). Note that the returned [Intent](/reference/android/content/Intent) simply suggests that the user picks this [StorageVolume](/reference/android/os/storage/StorageVolume) by default, but the user may select a different location. Callers must respect the user's chosen location, even if it is different from the originally requested location.
| Returns | |
|---|---|
| Intent | intent to Intent.ACTION_OPEN_DOCUMENT_TREE initially showing the contents of this StorageVolume This value cannot be null. |
describeContents
public int describeContents ()
Describe the kinds of special objects contained in this Parcelable instance's marshaled representation. For example, if the object will include a file descriptor in the output of [writeToParcel(Parcel,int)](/reference/android/os/Parcelable#writeToParcel%28android.os.Parcel,%20int%29), the return value of this method must include the[CONTENTS_FILE_DESCRIPTOR](/reference/android/os/Parcelable#CONTENTS%5FFILE%5FDESCRIPTOR) bit.
| Returns | |
|---|---|
| int | a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or CONTENTS_FILE_DESCRIPTOR |
equals
public boolean equals (Object obj)
Indicates whether some other object is "equal to" this one.
The equals method implements an equivalence relation on non-null object references:
- It is reflexive: for any non-null reference value
x,x.equals(x)should returntrue. - It is symmetric: for any non-null reference values
xandy,x.equals(y)should returntrueif and only ify.equals(x)returnstrue. - It is transitive: for any non-null reference values
x,y, andz, ifx.equals(y)returnstrueandy.equals(z)returnstrue, thenx.equals(z)should returntrue. - It is consistent: for any non-null reference values
xandy, multiple invocations ofx.equals(y)consistently returntrueor consistently returnfalse, provided no information used inequalscomparisons on the objects is modified. - For any non-null reference value
x,x.equals(null)should returnfalse.
An equivalence relation partitions the elements it operates on into equivalence classes; all the members of an equivalence class are equal to each other. Members of an equivalence class are substitutable for each other, at least for some purposes.
| Parameters | |
|---|---|
| obj | Object: This value may be null. |
| Returns | |
|---|---|
| boolean | true if this object is the same as the obj argument; false otherwise. |
getDescription
public String getDescription (Context context)
Returns a user-visible description of the volume.
| Parameters | |
|---|---|
| context | Context |
| Returns | |
|---|---|
| String | the volume description |
getDirectory
public File getDirectory ()
Returns the directory where this volume is currently mounted.
Direct filesystem access via this path has significant emulation overhead, and apps are instead strongly encouraged to interact with media on storage volumes via the [MediaStore](/reference/android/provider/MediaStore) APIs.
This directory does not give apps any additional access beyond what they already have via [MediaStore](/reference/android/provider/MediaStore).
| Returns | |
|---|---|
| File | directory where this volume is mounted, or null if the volume is not currently mounted. |
getOwner
public UserHandle getOwner ()
Returns the user that owns this volume
| Returns | |
|---|---|
| UserHandle | This value cannot be null. |
getUuid
public String getUuid ()
Gets the volume UUID, if any.
| Returns | |
|---|---|
| String | This value may be null. |
hashCode
public int hashCode ()
Returns a hash code value for the object. This method is supported for the benefit of hash tables such as those provided by[HashMap](/reference/java/util/HashMap).
The general contract of hashCode is:
- Whenever it is invoked on the same object more than once during an execution of a Java application, the
hashCodemethod must consistently return the same integer, provided no information used inequalscomparisons on the object is modified. This integer need not remain consistent from one execution of an application to another execution of the same application. - If two objects are equal according to the
[equals](/reference/java/lang/Object#equals%28java.lang.Object%29)method, then calling thehashCodemethod on each of the two objects must produce the same integer result. - It is not required that if two objects are unequal according to the
[equals](/reference/java/lang/Object#equals%28java.lang.Object%29)method, then calling thehashCodemethod on each of the two objects must produce distinct integer results. However, the programmer should be aware that producing distinct integer results for unequal objects may improve the performance of hash tables.
| Returns | |
|---|---|
| int | a hash code value for this object. |
isEmulated
public boolean isEmulated ()
Returns true if the volume is emulated.
| Returns | |
|---|---|
| boolean | is emulated |
isRemovable
public boolean isRemovable ()
Returns true if the volume is removable.
| Returns | |
|---|---|
| boolean | is removable |
toString
public String toString ()
Returns a string representation of the object.
| Returns | |
|---|---|
| String | a string representation of the object. |