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