Created
August 1, 2016 18:39
-
-
Save li-jkwok/e460a042326e8509ada9ec23ae677bdf to your computer and use it in GitHub Desktop.
Getting Total Disk Space Available for Android Device
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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; | |
} |
StatFs ? to root storage . . .
// 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 |
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
This code worked for me, but im still looking for an implementation that works on old versions