lightbulb_outline Please take our October 2018 developer survey. Start survey
added in API level 26

StorageStatsManager

public class StorageStatsManager
extends Object

java.lang.Object
   ↳ android.app.usage.StorageStatsManager


Access to detailed storage statistics. This provides a summary of how apps, users, and external/shared storage is utilizing disk space.

Note: no permissions are required when calling these APIs for your own package or UID. However, requesting details for any other package requires the android.Manifest.permission#PACKAGE_USAGE_STATS permission, which is a system-level permission that will not be granted to normal apps. Declaring that permission expresses your intention to use this API and an end user can then choose to grant this permission through the Settings application.

Instances of this class must be obtained using Context.getSystemService(Class) with the argument StorageStatsManager.class or Context.getSystemService(String) with the argument Context.STORAGE_STATS_SERVICE.

Summary

Public methods

long getFreeBytes(UUID storageUuid)

Return the free space on the requested storage volume.

long getTotalBytes(UUID storageUuid)

Return the total size of the underlying physical media that is hosting this storage volume.

ExternalStorageStats queryExternalStatsForUser(UUID storageUuid, UserHandle user)

Return shared/external storage statistics for a specific UserHandle on the requested storage volume.

StorageStats queryStatsForPackage(UUID storageUuid, String packageName, UserHandle user)

Return storage statistics for a specific package on the requested storage volume.

StorageStats queryStatsForUid(UUID storageUuid, int uid)

Return storage statistics for a specific UID on the requested storage volume.

StorageStats queryStatsForUser(UUID storageUuid, UserHandle user)

Return storage statistics for a specific UserHandle on the requested storage volume.

Inherited methods

Public methods

getFreeBytes

added in API level 26
public long getFreeBytes (UUID storageUuid)

Return the free space on the requested storage volume.

This value is best suited for visual display to end users, since it's designed to reflect both unused space and and cached space that could be reclaimed by the system.

Apps making logical decisions about disk space should always use StorageManager.getAllocatableBytes(UUID) instead of this value.

This method may take several seconds to complete, so it should only be called from a worker thread.

Value is a non-negative number of bytes.

Parameters
storageUuid UUID: the UUID of the storage volume you're interested in, such as StorageManager.UUID_DEFAULT.

This value must never be null.

Returns
long

Value is a non-negative number of bytes.

Throws
IOException when the storage device isn't present.

getTotalBytes

added in API level 26
public long getTotalBytes (UUID storageUuid)

Return the total size of the underlying physical media that is hosting this storage volume.

This value is best suited for visual display to end users, since it's designed to reflect the total storage size advertised in a retail environment.

Apps making logical decisions about disk space should always use File.getTotalSpace() instead of this value.

This method may take several seconds to complete, so it should only be called from a worker thread.

Value is a non-negative number of bytes.

Parameters
storageUuid UUID: the UUID of the storage volume you're interested in, such as StorageManager.UUID_DEFAULT.

This value must never be null.

Returns
long

Value is a non-negative number of bytes.

Throws
IOException when the storage device isn't present.

queryExternalStatsForUser

added in API level 26
public ExternalStorageStats queryExternalStatsForUser (UUID storageUuid, 
                UserHandle user)

Return shared/external storage statistics for a specific UserHandle on the requested storage volume.

Note: this API requires the android.Manifest.permission#PACKAGE_USAGE_STATS permission, which is a system-level permission that will not be granted to normal apps. Declaring that permission expresses your intention to use this API and an end user can then choose to grant this permission through the Settings application.

This method may take several seconds to complete, so it should only be called from a worker thread.

Parameters
storageUuid UUID: the UUID of the storage volume you're interested in, such as StorageManager.UUID_DEFAULT.

This value must never be null.

user UserHandle

This value must never be null.

Returns
ExternalStorageStats

This value will never be null.

Throws
IOException when the storage device isn't present.

queryStatsForPackage

added in API level 26
public StorageStats queryStatsForPackage (UUID storageUuid, 
                String packageName, 
                UserHandle user)

Return storage statistics for a specific package on the requested storage volume.

Note: no permissions are required when calling this API for your own package. However, requesting details for any other package requires the android.Manifest.permission#PACKAGE_USAGE_STATS permission, which is a system-level permission that will not be granted to normal apps. Declaring that permission expresses your intention to use this API and an end user can then choose to grant this permission through the Settings application.

Note: if the requested package uses the android:sharedUserId manifest feature, this call will be forced into a slower manual calculation path. If possible, consider always using queryStatsForUid(UUID, int), which is typically faster.

This method may take several seconds to complete, so it should only be called from a worker thread.

Parameters
storageUuid UUID: the UUID of the storage volume you're interested in, such as StorageManager.UUID_DEFAULT.

This value must never be null.

packageName String: the package name you're interested in.

This value must never be null.

user UserHandle: the user you're interested in.

This value must never be null.

Returns
StorageStats

This value will never be null.

Throws
when the requested package name isn't installed for the requested user.
IOException when the storage device isn't present.
PackageManager.NameNotFoundException

queryStatsForUid

added in API level 26
public StorageStats queryStatsForUid (UUID storageUuid, 
                int uid)

Return storage statistics for a specific UID on the requested storage volume.

Note: no permissions are required when calling this API for your own UID. However, requesting details for any other UID requires the android.Manifest.permission#PACKAGE_USAGE_STATS permission, which is a system-level permission that will not be granted to normal apps. Declaring that permission expresses your intention to use this API and an end user can then choose to grant this permission through the Settings application.

This method may take several seconds to complete, so it should only be called from a worker thread.

Parameters
storageUuid UUID: the UUID of the storage volume you're interested in, such as StorageManager.UUID_DEFAULT.

This value must never be null.

uid int: the UID you're interested in.

Returns
StorageStats

This value will never be null.

Throws
IOException when the storage device isn't present.

queryStatsForUser

added in API level 26
public StorageStats queryStatsForUser (UUID storageUuid, 
                UserHandle user)

Return storage statistics for a specific UserHandle on the requested storage volume.

Note: this API requires the android.Manifest.permission#PACKAGE_USAGE_STATS permission, which is a system-level permission that will not be granted to normal apps. Declaring that permission expresses your intention to use this API and an end user can then choose to grant this permission through the Settings application.

This method may take several seconds to complete, so it should only be called from a worker thread.

Parameters
storageUuid UUID: the UUID of the storage volume you're interested in, such as StorageManager.UUID_DEFAULT.

This value must never be null.

user UserHandle: the user you're interested in.

This value must never be null.

Returns
StorageStats

This value will never be null.

Throws
IOException when the storage device isn't present.