Android.os.Environment
Provides access to environment variables
Java.lang.Object |
|
|
Android.os.Environment |
|
Environment static method:
Method: Getdatadirectory ()
return: File
Explanation: Returns the directory of data
Method: Getdownloadcachedirectory ()
return: File
Explanation: Return to download buffer directory
Method: getExternalStorageDirectory ()
return: File
Explanation: Returning an extended storage directory (SDcard)
Method: getexternalstoragepublicdirectory (String type)
return: File
Explanation: Return a high-end public external memory directory to place certain types of files (from the web)
Method: Getrootdirectory ()
return: File
Explanation: Return to Android root directory
Method: Getexternalstoragestate ()
Returns: String
Explanation: Returns the current state of an external storage device
Getexternalstoragestate () returns the status of the string type constant:
Constant: Media_bad_removal
Value: "Bad_removal"
Explanation: Removed before SDcard was properly uninstalled
Constant: media_checking
Value: "Checking"
Explanation: Disk check is in progress
Constant: media_mounted
Value: "Mounted"
Explanation: is already mounted and has a readable writable permission
Constant: Media_mounted_read_only
Value: "Mounted_ro"
Explanation: already mounted, but with only readable permissions
Constant: Media_nofs
Value: "Nofs"
Explanation: The object is blank, or the file system does not support
Constant: media_removed
Value: "Removed"
Explanation: The extension device has been removed
Constant: media_shared
Value: "Shared"
Explanation: If SDcard is not mounted and shared via USB mass storage
Constant: media_unmountable
Value: "Unmountable"
Explanation: Can not mount any expansion device
Constant: media_unmounted
Value: "Unmounted"
Explanation: Already uninstalled
When using, just first determine the current state of SDcard and then get the SDcard directory (see source code)
---------------------------------------------------------------------------------------------------------
- SDcard operation
- Ublic void Sdcardtest () {
- Get extended SD card Device status
- String sdstatestring = Android.os.Environment.getExternalStorageState ();
- Have readable writable permissions
- if (Sdstatestring.equals (Android.os.Environment.MEDIA_MOUNTED)) {
- try {
- //Get the file directory for the extended storage device
- File Sdfile = android.os.Environment
- . getExternalStorageDirectory ();
- //Open File
- File MyFile = new File (Sdfile.getabsolutepath ()
- + File.separator + "MyFile.txt");
- //Determine if it exists, does not exist then creates
- 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)
- Have read-only permissions
- else if (sdstatestring
- . EndsWith (Android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) {
- //Get the file directory for the extended storage device
- File sdfile = Android.os.Environment.getExternalStorageDirectory ();
- //Create a file
- File MyFile = new File (Sdfile.getabsolutepath () + File.separator
- + "MyFile.txt");
- //Determine if the 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
Calculates the capacity size of the SDcard android.os.StatFs
A class that simulates the DF command of Linux to get the use of SD card and phone memory
Java.lang.Object |
|
|
Android.os.StatFs |
Construction Method:
StatFs (String Path)
Common methods:
Method: Getavailableblocks ()
return: int
Explanation: Returns the remaining blocks on the file system that can be used by the program
Method: Getblockcount ()
return: int
Explanation: Returns a total of blocks on the file system
Method: Getblocksize ()
return: int
Explanation: Returns the size unit of a block of file system byte
Method: Getfreeblocks ()
return: int
Explanation: All remaining blocks on the file system are returned, including reservations that are not accessible by the general program
Method: REStat (String path)
return: void
Explanation: Executes a file system Restat that is referenced by the object. (Google Translate)
To calculate the size and usage of the sdcard, you only need to get the total number of blocks that the SD card has, or the number of blocks left unused, multiplied by the size of each block is the corresponding capacity size of the unit byte. (See Code)
- public void Sdcardsizetest () {
- Get the current state of SDcard
- String sdcstring = Android.os.Environment.getExternalStorageState ();
- if (Sdcstring.equals (Android.os.Environment.MEDIA_MOUNTED)) {
- //Get sdcard file path
- File Pathfile = android.os.Environment
- . getExternalStorageDirectory ();
- Android.os.StatFs StatFs = new Android.os.StatFs (Pathfile.getpath ());
- //Get total block on SDcard
- Long ntotalblocks = Statfs.getblockcount ();
- //Get the size of each block on the SDcard
- Long nblocsize = Statfs.getblocksize ();
- //Get the number of blocks available for the program to use
- Long Navailablock = Statfs.getavailableblocks ();
- //Get the number of all remaining blocks (including block that is not available for General program reservation)
- Long Nfreeblock = Statfs.getfreeblocks ();
- //Calculate SDcard Total capacity MB
- Long nsdtotalsize = ntotalblocks * Nblocsize/ 1024x768/ 1024;
- //Calculate sdcard remaining size MB
- Long nsdfreesize = Navailablock * Nblocsize/ 1024x768/ 1024;
- }//end of If
- End of Func
Reprinted from: http://crackren.javaeye.com/blog/747121
"Go" Android sdcard operation (file read/write, capacity calculation)