Android get system or sdcard remaining space information (GO)

Source: Internet
Author: User

Android.os under the Statfs class is mainly used to obtain the state of the file system, to obtain the size of the SD card and the remaining space, the system internal space is the size of the/system and the remaining space and so on.
Look under the Read SD card:
Java code

1 voidReadsdcard () {2String State =environment.getexternalstoragestate ();3         if(Environment.MEDIA_MOUNTED.equals (state)) {4File Sdcarddir =environment.getexternalstoragedirectory ();5StatFs SF =NewStatFs (Sdcarddir.getpath ());6             LongBlockSize =sf.getblocksize ();7             LongBlockcount =Sf.getblockcount ();8             LongAvailcount =sf.getavailableblocks ();9LOG.D ("", "Block Size:" + blocksize+ ", block number:" + blockcount+ ", Total size:" +blocksize*blockcount/1024+ "KB"); TenLOG.D ("", "number of available blocks::" + availcount+ ", Remaining space:" + availcount*blocksize/1024+ "KB");  One         }     A}

And then look at the internal space of the system reading:
Java code

1 voidReadsystem () {2File root =environment.getrootdirectory ();3StatFs SF =NewStatFs (Root.getpath ());4         LongBlockSize =sf.getblocksize ();5         LongBlockcount =Sf.getblockcount ();6         LongAvailcount =sf.getavailableblocks ();7LOG.D ("", "Block Size:" + blocksize+ ", block number:" + blockcount+ ", Total size:" +blocksize*blockcount/1024+ "KB"); 8LOG.D ("", "number of available blocks::" + availcount+ ", Available size:" + availcount*blocksize/1024+ "KB"); 9}

Statfs gets all the blocks, here I explain the concept of block:
1. The block size on the hardware should be "sector size" and the Linux sector is 512byte
2. Block size with file system partition, "block size", can be viewed with tools
3. Block size for partitions without file system, also known as "block Size", refers to a size of
The block size of 4.Kernel buffer cache is "block size", and most PCs are 1024
5. Disk Partition "cylinder size", can be viewed with Fdisk.
Our block size here is the second case, the general SD card is FAT32 file system, block size is 4096.
This will allow you to know the total size and available size of your phone's internal storage and SD card storage space.

If s

1  Public classSdcardsizeutil {2      Public Static BooleanIsavaiablespace (intsizemb) {3         BooleanIshasspace =false;4         if(Android.os.Environment.getExternalStorageState (). Equals (5 Android.os.Environment.MEDIA_MOUNTED)) {6String SDcard =environment.getexternalstoragedirectory (). GetPath ();7StatFs StatFs =NewStatFs (sdcard);8             LongBlockSize =statfs.getblocksize ();9             LongBlocks =statfs.getavailableblocks ();Ten             LongAvailablespare = (Blocks * blockSize)/(1024 * 1024); OneLOG.D ("Remaining space", "availablespare =" +availablespare); A             if(Availablespare >sizemb) { -Ishasspace =true; -             } the         } -         returnIshasspace; -     } -}

The remaining space of the D card is less than a value that returns false if there is enough space to return true.

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.