Skip to content

Instantly share code, notes, and snippets.

@li-jkwok
Created August 1, 2016 18:39
Show Gist options
  • Save li-jkwok/e460a042326e8509ada9ec23ae677bdf to your computer and use it in GitHub Desktop.
Save li-jkwok/e460a042326e8509ada9ec23ae677bdf to your computer and use it in GitHub Desktop.
Getting Total Disk Space Available for Android Device
/**
* Get the total disk space available on this Android Device
*
* @return total size (in bytes) that is the total disk space avaialble on the device.
*/
public static long getTotalDiskSpace() {
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath());
long totalDiskSpace = statFs.getBlockCount() * statFs.getBlockSize();
return totalDiskSpace;
}
@m-asadullah
Copy link

// Get StatFs for the root storage
StatFs statFs = new StatFs(Environment.getRootDirectory().getPath());

// Get StatFs for the external storage
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getPath());

Root Storage (Environment.getRootDirectory().getPath()):

  • Definition: This refers to the root directory of the Android system. It's where the system files are stored.
  • Usage: As a developer, you generally don't need to interact with this directory, since modifying system files can harm the device's stability and integrity.
  • Path Example: Typically something like /system.

External Storage (Environment.getExternalStorageDirectory().getPath()):

  • Definition: This refers to the primary shared/external storage directory where users can store their media and files. It's accessible by both the user and apps, and is often what people refer to as the "SD card" (though it may be internal to the device).
  • Usage: You use this directory to store user data that needs to be shared across apps or that users might want to access directly (e.g., pictures, videos, downloads).
  • Path Example: Typically something like /storage/emulated/0.
Storage Type Access Method Typical Path Usage
Root Storage Environment.getRootDirectory().getPath() /system System files, not usually modified by apps
External Storage
(Phone: default storage available for user)
Environment.getExternalStorageDirectory().getPath() /storage/emulated/0 User files, media, shared app data
External Storage
(SDCard)
Environment.getExternalStorageDirectory().getPath() /storage/emulated/1 User files, media in MicroSD card

@wiraSan1262
Copy link

i was commenting @mohammednawas8

for below Build.VERSION_CODES.O

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment