Android obtains the size of internal and external memory.

Source: Internet
Author: User

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"
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.