Added in API level 24

StorageVolume

class StorageVolume : Parcelable
kotlin.Any
   ↳ android.os.storage.StorageVolume

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() 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:

It can be obtained through StorageManager#getStorageVolumes() and StorageManager#getPrimaryStorageVolume() and also as an extra in some broadcasts (see EXTRA_STORAGE_VOLUME).

See Environment#getExternalStorageDirectory() for more info about shared/external storage semantics.

Summary

Constants
static String

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
Public methods
Intent?
createAccessIntent(directoryName: String!)

Builds an intent to give access to a standard storage directory or entire volume after obtaining the user's approval.

Intent

Builds an Intent#ACTION_OPEN_DOCUMENT_TREE to allow the user to grant access to any directory subtree (or entire volume) from the android.provider.DocumentsProviders available on the device.

Int

Boolean
equals(other: Any?)

Indicates whether some other object is "equal to" this one.

String!

Returns a user-visible description of the volume.

File?

Returns the directory where this volume is currently mounted.

String?

Return the volume name that can be used to interact with this storage device through MediaStore.

UserHandle

Returns the user that owns this volume

String!

Returns the current state of the volume.

UUID?

Gets the converted volume UUID.

String?

Gets the volume UUID, if any.

Int

Boolean

Returns true if the volume is emulated.

Boolean

Returns true if the volume is the primary shared/external storage, which is the volume backed by Environment#getExternalStorageDirectory().

Boolean

Returns true if the volume is removable.

String

Unit
writeToParcel(parcel: Parcel, flags: Int)

Properties
static Parcelable.Creator<StorageVolume!>

Constants

Public methods

createAccessIntent

Added in API level 24
Deprecated in API level 29
fun createAccessIntent(directoryName: String!): Intent?

Deprecated: Callers should migrate to using Intent#ACTION_OPEN_DOCUMENT_TREE instead. Launching this Intent on devices running android.os.Build.VERSION_CODES#Q or higher, will immediately finish with a result code of android.app.Activity#RESULT_CANCELED.

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 the onActivityResult method.

To gain access to descendants (child, grandchild, etc) documents, use DocumentsContract#buildDocumentUriUsingTree(Uri, String), or DocumentsContract#buildChildDocumentsUriUsingTree(Uri, String) with the returned URI.

If your application only needs to store internal data, consider using Context.getExternalFilesDirs, Context#getExternalCacheDirs(), or Context#getExternalMediaDirs(), 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 android.Manifest.permission#READ_EXTERNAL_STORAGE and android.Manifest.permission#WRITE_EXTERNAL_STORAGE 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, or Environment#DIRECTORY_DOCUMENTS, or null to request access to the entire volume.
Return
Intent? intent to request access, or null if the requested directory is invalid for that volume.

createOpenDocumentTreeIntent

Added in API level 29
fun createOpenDocumentTreeIntent(): Intent

Builds an Intent#ACTION_OPEN_DOCUMENT_TREE to allow the user to grant access to any directory subtree (or entire volume) from the android.provider.DocumentsProviders available on the device. The initial location of the document navigation will be the root of this StorageVolume. Note that the returned Intent simply suggests that the user picks this 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.

Return
Intent intent to Intent#ACTION_OPEN_DOCUMENT_TREE initially showing the contents of this StorageVolume This value cannot be null.

describeContents

Added in API level 24
fun describeContents(): Int
Return
Int a bitmask indicating the set of special object types marshaled by this Parcelable object instance. Value is either 0 or android.os.Parcelable#CONTENTS_FILE_DESCRIPTOR

equals

Added in API level 24
fun equals(other: Any?): Boolean

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 return true.
  • It is symmetric: for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.
  • It is transitive: for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true, then x.equals(z) should return true.
  • It is consistent: for any non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or consistently return false, provided no information used in equals comparisons on the objects is modified.
  • For any non-null reference value x, x.equals(null) should return false.

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 This value may be null.
Return
Boolean true if this object is the same as the obj argument; false otherwise.

getDescription

Added in API level 24
fun getDescription(context: Context!): String!

Returns a user-visible description of the volume.

Return
String! the volume description

getDirectory

Added in API level 30
fun getDirectory(): File?

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 APIs.

This directory does not give apps any additional access beyond what they already have via MediaStore.

Return
File? directory where this volume is mounted, or null if the volume is not currently mounted.

getMediaStoreVolumeName

Added in API level 30
fun getMediaStoreVolumeName(): String?

Return the volume name that can be used to interact with this storage device through MediaStore.

Return
String? opaque volume name, or null if this volume is not indexed by MediaStore.

getOwner

Added in API level 33
fun getOwner(): UserHandle

Returns the user that owns this volume

Return
UserHandle This value cannot be null.

getStorageUuid

Added in API level 31
fun getStorageUuid(): UUID?

Gets the converted volume UUID. If a valid UUID is returned, it is compatible with other APIs that make use of UUID like android.os.storage.StorageManager#allocateBytes and android.content.pm.ApplicationInfo#storageUuid

Return
UUID? the UUID for the volume or null for "portable" storage devices which haven't been adopted.

See Also

    getUuid

    Added in API level 24
    fun getUuid(): String?

    Gets the volume UUID, if any.

    Return
    String? This value may be null.

    hashCode

    Added in API level 24
    fun hashCode(): Int
    Return
    Int a hash code value for this object.

    isEmulated

    Added in API level 24
    fun isEmulated(): Boolean

    Returns true if the volume is emulated.

    Return
    Boolean is emulated

    isPrimary

    Added in API level 24
    fun isPrimary(): Boolean

    Returns true if the volume is the primary shared/external storage, which is the volume backed by Environment#getExternalStorageDirectory().

    isRemovable

    Added in API level 24
    fun isRemovable(): Boolean

    Returns true if the volume is removable.

    Return
    Boolean is removable

    toString

    Added in API level 24
    fun toString(): String
    Return
    String a string representation of the object.

    writeToParcel

    Added in API level 24
    fun writeToParcel(
        parcel: Parcel,
        flags: Int
    ): Unit
    Parameters
    dest The Parcel in which the object should be written. This value cannot be null.
    flags Int: Additional flags about how the object should be written. May be 0 or PARCELABLE_WRITE_RETURN_VALUE. Value is either 0 or a combination of android.os.Parcelable#PARCELABLE_WRITE_RETURN_VALUE, and android.os.Parcelable.PARCELABLE_ELIDE_DUPLICATES

    Properties

    CREATOR

    Added in API level 24
    static val CREATOR: Parcelable.Creator<StorageVolume!>