Problem summary (7)-SD card and/data Partition usage Detection

Source: Internet
Author: User

Used to obtain the usage of the SD card and system/data Partition, including the total space size, available space, and used space.

[Java]
Package com. schogini. dhcp;
 
Import java. io. File;
 
Import android. content. Context;
Import android. OS. Environment;
Import android. OS. StatFs;
 
Public class CheckStore {
Private static String notice;

Public static String checkSD (Context contect ){

File data = Environment. getDataDirectory (); // obtain the data path.
StatFs data_stat = new StatFs (data. getPath (); // create a StatFs object
Long data_blockSize = data_stat.getBlockSize (); // obtain the block size
Float data_totalBlocks = data_stat.getBlockCount (); // gets the number of blocks.
Int data_sizeInMb = (int) (data_blockSize * data_totalBlocks)/1024/1024; // calculates the total capacity
Long data_availableBlocks = data_stat.getAvailableBlocks (); // obtain the number of available blocks.
Float data_percent = (int) (data_blockSize * data_availableBlocks)/1024/1024; // calculate the available capacity
Notice = "FLASH usage: \ n total capacity:" + data_sizeInMb + "M. \ n used: "+ (data_sizeInMb-data_percent +" \ n available: "+ data_percent +" M. ");

If (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED )){
File filePath = Environment. getExternalStorageDirectory (); // obtain the path of the sd card

StatFs stat = new StatFs (filePath. getPath (); // create a StatFs object
Long blockSize = stat. getBlockSize (); // obtain the block size
Float totalBlocks = stat. getBlockCount (); // gets the number of blocks.
Int sizeInMb = (int) (blockSize * totalBlocks)/1024/1024; // calculate the total capacity
Long availableBlocks = stat. getAvailableBlocks (); // number of available Blocks
Float percent = (int) (blockSize * availableBlocks)/1024/1024; // calculate the available capacity

Notice = notice + "\ nSD card usage: \ n total capacity:" + sizeInMb + "M. \ n used: "+ (sizeInMb-percent +" M \ n available: "+ percent +" M. ");
Return notice;
}
Else {
Notice = notice + "\ nSD card usage: SD card not inserted ";
Return notice;
}
 
}
}
Package com. schogini. dhcp;

Import java. io. File;

Import android. content. Context;
Import android. OS. Environment;
Import android. OS. StatFs;

Public class CheckStore {
Private static String notice;
 
Public static String checkSD (Context contect ){

File data = Environment. getDataDirectory (); // obtain the data path.
StatFs data_stat = new StatFs (data. getPath (); // create a StatFs object
Long data_blockSize = data_stat.getBlockSize (); // obtain the block size
Float data_totalBlocks = data_stat.getBlockCount (); // gets the number of blocks.
Int data_sizeInMb = (int) (data_blockSize * data_totalBlocks)/1024/1024; // calculates the total capacity
Long data_availableBlocks = data_stat.getAvailableBlocks (); // obtain the number of available blocks.
Float data_percent = (int) (data_blockSize * data_availableBlocks)/1024/1024; // calculate the available capacity
Notice = "FLASH usage: \ n total capacity:" + data_sizeInMb + "M. \ n used: "+ (data_sizeInMb-data_percent +" \ n available: "+ data_percent +" M. ");

If (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED )){
File filePath = Environment. getExternalStorageDirectory (); // obtain the path of the sd card

StatFs stat = new StatFs (filePath. getPath (); // create a StatFs object
Long blockSize = stat. getBlockSize (); // obtain the block size
Float totalBlocks = stat. getBlockCount (); // gets the number of blocks.
Int sizeInMb = (int) (blockSize * totalBlocks)/1024/1024; // calculate the total capacity
Long availableBlocks = stat. getAvailableBlocks (); // number of available Blocks
Float percent = (int) (blockSize * availableBlocks)/1024/1024; // calculate the available capacity

Notice = notice + "\ nSD card usage: \ n total capacity:" + sizeInMb + "M. \ n used: "+ (sizeInMb-percent +" M \ n available: "+ percent +" M. ");
Return notice;
}
Else {
Notice = notice + "\ nSD card usage: SD card not inserted ";
Return notice;
}

}
}
Author: agods
 

Related Article

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.