Environment and Statfs in Android get system/sdcard storage space size

Source: Internet
Author: User

Recently, I think of the knowledge of Android development. Long time no use, have forgotten almost the same, today to check the information back to pick up, by the way write down to help the students need.

Let's start with the two classes of environment and STATFS, and then describe their specific uses.

1, Environment class:
Environment is a class that provides access to environment variables.


Environment include constants:
Media_bad_removal
Explanation: Return Getexternalstoragestate (), indicating that SDcard was removed before being unloaded
Media_checking
Explanation: Returns Getexternalstoragestate (). Indicates that the object is checking the disk.

 
media_mounted 
Explanation: Returns Getexternalstoragestate () indicating whether the object exists and has read/write permission  
Media_mounted_ read_only 
Explanation: Returns Getexternalstoragestate (). Indicates that the object permission is read-only  ,
media_nofs 
Explanation: Returns Getexternalstoragestate (), indicating that the object is blank or is using an unsupported file system.  
media_removed 
Explanation: Returns Getexternalstoragestate (), assuming there is no sdcard return  
media_shared 
Explanation: Returns Getexternalstoragestate (). Suppose SDcard is not installed. and return   via USB mass storage share;
media_unmountable 
Explanation: Return Getexternalstoragestate (), return sdcard cannot be installed assuming SDcard is present but cannot be installed  
media_unmounted 
Explanation: Return Getexternalstoragestate (), return sdcard unloaded assuming SDcard is present but not installed  
Environment frequent usage:  
Method: Getdatadirectory ()  
Explanation: Returns File, gets the Android data folder.  
Method: Getdownloadcachedirectory ()  
Explanation: Returns File. Get the Android download/cache content folder.


Method: getExternalStorageDirectory ()
Explanation: Return File, Get External storage folder that is SDcard
Method: Getexternalstoragepublicdirectory (String type)
Explanation: Return file, take a high-end public external memory folder to place certain types of files
Method: Getexternalstoragestate ()
Explanation: Returns File. Get the current state of an external storage device
Method: Getrootdirectory ()
Explanation: Return File, get the root folder of Android
2, StatFs class:
StatFs A class that simulates the DF command of Linux to get the use of SD card and phone memory
StatFs Frequent usage:
Getavailableblocks ()
Explanation: Returns INT. Get the currently available storage space
Getblockcount ()
Explanation: Returns an Int that gets the number of file systems available for the zone
Getblocksize ()
Explanation: Returns INT, size, in bytes, of a file system
Getfreeblocks ()
Explanation: Returns INT, the space remaining in the block area
REStat (String Path)
Explanation: Run a file system that is referenced by the object


/** when storing files, it is usually necessary to know the storage size of the inside or sdcard of the system in order to ensure sufficient amount of space left.

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;


/**
* External storage is available
* @return
*/
Static public Boolean externalmemoryavailable () {
Return Android.os.Environment.getExternalStorageState (). Equals (
Android.os.Environment.MEDIA_MOUNTED);
}


/**
* Get the amount of free space inside your 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;
}


/**
* Get the size of your phone's internal space
* @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;
}


/**
* Get the size of your phone's external free space
* @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;
}
}


/**
* Get the size of your phone's external space
* @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 ();
}
}



Environment and Statfs in Android get system/sdcard storage space size

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.