Android. OS. Environment
Provide access environment variables
Java. Lang. Object |
|
|
Android. OS. Environment |
|
Environment static method:
Method: getdatadirectory ()
Return Value: File
Explanation: returned data directory
Method: getdownloadcachedirectory ()
Return Value: File
Explanation: Return to the download buffer directory
Method: getexternalstoragedirectory ()
Return Value: File
Explanation: return the extended storage directory (sdcard)
Method: getexternalstoragepublicdirectory (string type)
Return Value: File
Explanation: return a high-end public external memory directory to place some types of files (from the Internet)
Method: getrootdirectory ()
Return Value: File
Explanation: returns the android root directory.
Method: getexternalstoragestate ()
Return Value: String
Explanation: returns the current status of the external storage device.
Getexternalstoragestate() The returned status is a constant of the string type:
Constant: media_bad_removal
Value: "bad_removal"
Explanation: removed before sdcard is correctly uninstalled
Constant: media_checking
Value: "checking"
Explanation: Checking Disk
Constant: media_mounted
Value: "mounted"
Explanation: it has been mounted and has the readable and writable permissions.
Constant: media_mounted_read_only
Value: "mounted_ro"
Explanation: it has been mounted, but only has the read permission.
Constant: media_nofs
Value: "nofs"
Explanation: the object is blank or not supported by the file system.
Constant: media_removed
Value: "removed"
Explanation: Extended devices have been removed.
Constant: media_shared
Value: "shared"
Explanation: If the sdcard is not mounted and shared through USB large capacity storage
Constant: media_unmountable
Value: "unmountable"
Explanation: No extended devices can be mounted.
Constant: media_unmounted
Value: "unmounted"
Explanation: uninstalled
You only need to judge the current status of the sdcard before obtaining the directory of the sdcard (see source code)
Bytes ---------------------------------------------------------------------------------------------------------
- // Sdcard operation
- Ublic void sdcardtest (){
- // Get the status of the extended SD card device
- String sdstatestring = Android. OS. environment. getexternalstoragestate ();
- // Read and Write Permissions
- If (sdstatestring. Equals (Android. OS. environment. media_mounted )){
- Try {
- // Obtain the file directory of the extended storage device
- File sdfile = Android. OS. Environment
- . Getexternalstoragedirectory ();
- // Open the file
- File myfile = new file (sdfile. getabsolutepath ()
- + File. Separator + "myfile.txt ");
- // Determine whether the object exists. If the object does not exist, the object is created.
- If (! Myfile. exists ()){
- Myfile. createnewfile ();
- }
- // Write data
- String szouttext = "Hello, world! ";
- Fileoutputstream outputstream = new fileoutputstream (myfile );
- Outputstream. Write (szouttext. getbytes ());
- Outputstream. Close ();
- } Catch (exception e ){
- // Todo: handle exception
- } // End of try
- } // End of IF (media_mounted)
- // Read-only permission
- Else if (sdstatestring
- . Endswith (Android. OS. environment. media_mounted_read_only )){
- // Obtain the file directory of the extended storage device
- File sdfile = Android. OS. environment. getexternalstoragedirectory ();
- // Create an object
- File myfile = new file (sdfile. getabsolutepath () + file. Separator
- + "Myfile.txt ");
- // Determine whether a file exists
- If (myfile. exists ()){
- Try {
- // Read data
- Fileinputstream inputstream = new fileinputstream (myfile );
- Byte [] buffer = new byte [1024];
- Inputstream. Read (buffer );
- Inputstream. Close ();
- } Catch (exception e ){
- // Todo: handle exception
- } // End of try
- } // End of IF (myfile)
- } // End of IF (media_mounted_read_only)
- // End of func
Calculate the size of an sdcard
Android. OS. statfs
A class that simulates the DF command of Linux to obtain the usage of SD card and mobile phone memory.
Java. Lang. Object |
|
|
Android. OS. statfs |
Constructor:
Statfs (string path)
Public method:
Method: getavailableblocks ()
Return Value: int
Explanation: return the remaining blocks on the file system that can be used by the program.
Method: getblockcount ()
Return Value: int
Explanation: returns the total number of blocks in the file system.
Method: getblocksize ()
Return Value: int
Explanation: returns the size unit of a block in the file system, in bytes.
Method: getfreeblocks ()
Return Value: int
Explanation: return all the remaining parts of the file system, including reserved, which cannot be accessed by General programs.
Method: restat (string path)
Return: void
Explanation: execute a file system named by this object. (Google Translate)
To calculate the size and usage of an sdcard, you only need to obtain the total number of blocks or the remaining useless blocks on the SD card, and multiply the size of each block by the corresponding size (in bytes. (See the code)
Public void sdcardsizetest () {</P> <p> // get the current status of the sdcard <br/> string sdcstring = android. OS. environment. getexternalstoragestate (); </P> <p> If (sdcstring. equals (Android. OS. environment. media_mounted) {</P> <p> // obtain the path of the sdcard file <br/> file pathfile = android. OS. environment <br/>. getexternalstoragedirectory (); </P> <p> android. OS. statfs = new android. OS. statfs (pathfile. getpath (); </P> <p> // obtain the total number of blocks on the sdcard. <br/> long ntotalblocks = statfs. getblockcount (); </P> <p> // obtain the size of each block on the sdcard <br/> long nblocsize = statfs. getblocksize (); </P> <p> // obtain the number of blocks available for the program. <br/> long navailablock = statfs. getavailableblocks (); </P> <p> // obtain the number of all remaining blocks (including the blocks that cannot be used by the reserved General Program) <br/> long nfreeblock = statfs. getfreeblocks (); </P> <p> // calculate the total sdcard capacity in MB <br/> long nsdtotalsize = ntotalblocks * nblocsize/1024/1024; </P> <p> // calculate the remaining size of the sdcard in MB <br/> long nsdfreesize = navailablock * nblocsize/1024/1024; <br/>}// end of if