Android Stats Manager
From Android O (API 26) StorageStatsManager class is available to Access detailed storage statistics. It provides a summary of how apps, users, and external/shared storage is utilizing disk space.
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 the end user can then choose to grant this permission through the Settings application.
You can easily get Storage Volume Statistics using the following piece of code. It gives you free space on the requested storage volume and the total size of the underlying physical media that is hosting this storage volume.
Query Stats For Package
You can get storage statistics for a specific package on the requested storage volume. To get storage stats for a particular package, you will need UserHandler, UUID and package name.
UUID represents an immutable universally unique identifier (UUID). A UUID represents a 128-bit value.
The following piece of code will fetch the required data:
getAppBytes Return the size of app. This includes APK files, optimized compiler output, and unpacked native libraries.
getDataBytes Return the size of all data. This includes files stored under getDataDir(), getCacheDir(), getCodeCacheDir().
getCacheBytes Return the size of all cached data. This includes files stored under getCacheDir() and getCodeCacheDir().
If you want a demo project or source code on GitHub, check out the following: