The StatFs class in Android. OS is used to obtain the status of the file system, the size and remaining space of the SD card, and the size and remaining space of the/system.
Read the SD card:
Void readSDCard (){
String state = Environment. getExternalStorageState ();
If (Environment. MEDIA_MOUNTED.equals (state )){
File sdcardDir = Environment. getExternalStorageDirectory ();
StatFs sf = new StatFs (sdcardDir. getPath ());
Long blockSize = sf. getBlockSize ();
Long blockCount = sf. getBlockCount ();
Long availCount = sf. getAvailableBlocks ();
Log. d ("", "block Size:" + blockSize + ", number of blocks:" + blockCount + ", total size:" + blockSize * blockCount/1024 + "KB ");
Log. d ("", "number of available blocks:" + availCount + ", available space:" + availCount * blockSize/1024 + "KB ");
}
}
Then let's take a look at the reading of the internal space of the system:
Void readSystem (){
File root = Environment. getRootDirectory ();
StatFs sf = new StatFs (root. getPath ());
Long blockSize = sf. getBlockSize ();
Long blockCount = sf. getBlockCount ();
Long availCount = sf. getAvailableBlocks ();
Log. d ("", "block Size:" + blockSize + ", number of blocks:" + blockCount + ", total size:" + blockSize * blockCount/1024 + "KB ");
Log. d ("", "number of available blocks:" + availCount + ", available size:" + availCount * blockSize/1024 + "KB ");
}
StatFs obtains block units. Here I will explain the concept of block:
1. The block size on the hardware should be "sector size", and the linux sector size is 512 bytes.
2. The block size of the partition with a file system is "block size", which is different in size. You can use a tool to view it.
3. block size of a partition without a file system, also known as "block size". The size refers to 1024 bytes.
4. The block size of the Kernel buffer cache is "block size". Most PCs are 1024
5. "cylinder size" of the disk partition, which can be viewed using fdisk.
The block size here is the second case. Generally, the SD card is a fat32 file system, and the block size is 4096.
In this way, you can know the total size and available size of the phone's internal storage space and SD card storage space.
Author "liangoogle"