In Android, Environment and StatFs obtain the size of the system/SDCard storage space, statfssdcard

Source: Internet
Author: User

In Android, Environment and StatFs obtain the size of the system/SDCard storage space, statfssdcard

I recently remembered the knowledge of Android development. I haven't used it for a long time, and I forgot about it. I checked the information for a while and found it back. By the way, I wrote it down to help students who needed it.

First, let's talk about the Environment and StatFs classes, and then introduce their detailed usage methods.

1. Environment class:
Environment is a class that provides access to Environment variables.
Environment contains constants:
MEDIA_BAD_REMOVAL
Explanation: getExternalStorageState () is returned, indicating that the SDCard has been removed before it is detached.
MEDIA_CHECKING
Explanation: getExternalStorageState () is returned, indicating that the object is checking the disk.
MEDIA_MOUNTED
Explanation: getExternalStorageState () is returned, indicating whether the object exists and has read/write permissions.
MEDIA_MOUNTED_READ_ONLY
Explanation: getExternalStorageState () is returned, indicating that the object permission is read-only.
MEDIA_NOFS
Explanation: getExternalStorageState () is returned, indicating that the object is blank or unsupported file system is being used.
MEDIA_REMOVED
Explanation: getExternalStorageState () is returned. If no SDCard exists
MEDIA_SHARED
Explanation: getExternalStorageState () is returned. If the SDCard is not installed and shared by USB
MEDIA_UNMOUNTABLE
Explanation: getExternalStorageState () is returned, and SDCard cannot be installed if it exists but cannot be installed.
MEDIA_UNMOUNTED
Explanation: getExternalStorageState () is returned, and the SDCard has been detached. If the SDCard exists but is not installed
Common Environment methods:
Method: getDataDirectory ()
Explanation: return a File to obtain the Android data directory.
Method: getDownloadCacheDirectory ()
Explanation: return a File to obtain the Android download/cache content directory.
Method: getExternalStorageDirectory ()
Explanation: The returned File gets the external storage directory, that is, SDCard.
Method: getExternalStoragePublicDirectory (String type)
Explanation: The returned File is a high-end public external memory directory to place some types of files.
Method: getExternalStorageState ()
Explanation: returns a File to obtain the current status of the external storage device.
Method: getRootDirectory ()
Explanation: returns a File to obtain the Android root directory.
2. StatFs class:
StatFs is a class that simulates df commands in linux to obtain the usage of SD card and mobile phone memory.
Common StatFs methods:
GetAvailableBlocks ()
Explanation: Int is returned to obtain the currently available bucket.
GetBlockCount ()
Explanation: Int is returned to obtain the number of available file systems in the region.
GetBlockSize ()
Explanation: Int is returned, in bytes. It is a file system.
GetFreeBlocks ()
Explanation: Int is returned, indicating the remaining space in the block area.
Restat (String path)
Explanation: execute a file system referenced by this object.


/** When storing files, you usually need to know the storage size inside the system or sdcard to ensure sufficient available space. The following provides a tool class */

Package com. neverno. util;


Import java. io. File;


Import android. OS. Environment;
Import android. OS. StatFs;


Public class MemoryStatus {
Static final int ERROR =-1;


/**
* Whether external storage is available
* @ Return
*/
Static public boolean externalMemoryAvailable (){
Return android. OS. Environment. getExternalStorageState (). equals (
Android. OS. Environment. MEDIA_MOUNTED );
}


/**
* Obtain the internal available space of the mobile phone.
* @ Return
*/
Static public long getAvailableInternalMemorySize (){
File path = Environment. getDataDirectory ();
StatFs stat = new StatFs (path. getPath ());
Long blockSize = stat. getBlockSize ();
Long availableBlocks = stat. getAvailableBlocks ();
Return availableBlocks * blockSize;
}


/**
* Obtain the internal space of the mobile phone.
* @ Return
*/
Static public long getTotalInternalMemorySize (){
File path = Environment. getDataDirectory ();
StatFs stat = new StatFs (path. getPath ());
Long blockSize = stat. getBlockSize ();
Long totalBlocks = stat. getBlockCount ();
Return totalBlocks * blockSize;
}


/**
* Obtain the external available space of the mobile phone
* @ Return
*/
Static public long getAvailableExternalMemorySize (){
If (externalMemoryAvailable ()){
File path = Environment. getExternalStorageDirectory ();
StatFs stat = new StatFs (path. getPath ());
Long blockSize = stat. getBlockSize ();
Long availableBlocks = stat. getAvailableBlocks ();
Return availableBlocks * blockSize;
} Else {
Return ERROR;
}
}


/**
* Obtain the external space of the mobile phone.
* @ Return
*/
Static public long getTotalExternalMemorySize (){
If (externalMemoryAvailable ()){
File path = Environment. getExternalStorageDirectory ();
StatFs stat = new StatFs (path. getPath ());
Long blockSize = stat. getBlockSize ();
Long totalBlocks = stat. getBlockCount ();
Return totalBlocks * blockSize;
} Else {
Return ERROR;
}
}


Static public String formatSize (long size ){
String suffix = null;


If (size> = 1024 ){
Suffix = "KiB ";
Size/= 1024;
If (size> = 1024 ){
Suffix = "MiB ";
Size/= 1024;
}
}


StringBuilder resultBuffer = new StringBuilder (Long. toString (size ));
Int commaOffset = resultBuffer. length ()-3;
While (commaOffset> 0 ){
ResultBuffer. insert (commaOffset ,',');
CommaOffset-= 3;
}


If (suffix! = Null)
ResultBuffer. append (suffix );
Return resultBuffer. toString ();
}
}



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.