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

Source: Internet
Author: User

Recently think of the knowledge of Android development, long time no use, have forgotten almost, today to review a bit of information back to pick up, by the way write down to help the students in need.

Let's start with the two classes of environment and STATFS, and then describe their detailed use methods.

1, Environment class:
Environment is a class that provides access to environment variables.
Environment contains constants:
Media_bad_removal
Explanation: Return Getexternalstoragestate (), indicating that SDcard was removed before being unloaded
Media_checking
Explanation: Returns Getexternalstoragestate (), indicating that the object is checking the disk.
media_mounted
Explanation: Returns Getexternalstoragestate () indicating whether the object exists and has read/write permissions
Media_mounted_read_only
Explanation: Returns Getexternalstoragestate (), indicating 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 () if no sdcard is present
Media_shared
Explanation: Returns Getexternalstoragestate () if SDcard is not installed and is returned through a USB mass storage share
Media_unmountable
Explanation: Return Getexternalstoragestate (), return sdcard cannot be installed if SDcard is present but cannot be installed
media_unmounted
Explanation: Return Getexternalstoragestate (), return sdcard removed if SDcard is present but not installed
Environment Common methods:
Method: Getdatadirectory ()
Explanation: Return File to get the Android data directory.
Method: Getdownloadcachedirectory ()
Explanation: Return File to get the Android download/cache content directory.
Method: getExternalStorageDirectory ()
Explanation: Return File, Get External storage directory is SDcard
Method: Getexternalstoragepublicdirectory (String type)
Explanation: Return file, take a high-end common external memory directory to place certain types of files
Method: Getexternalstoragestate ()
Explanation: Returns File to get the current state of the external storage device
Method: Getrootdirectory ()
Explanation: Return File, get the root directory 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 Common methods:
Getavailableblocks ()
Explanation: Returns an Int to 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: Executes a file system that is referenced by the object


/** when storing files, it is often necessary to know the storage size of the inside or sdcard of the system in order to ensure sufficient amount of space remaining. A tool class is provided below */

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.