"Go" Android sdcard operation (file read/write, capacity calculation)

Source: Internet
Author: User

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)

---------------------------------------------------------------------------------------------------------

  1. SDcard operation
  2. Ublic void Sdcardtest () {
  3. Get extended SD card Device status
  4. String sdstatestring = Android.os.Environment.getExternalStorageState ();
  5. Have readable writable permissions
  6. if (Sdstatestring.equals (Android.os.Environment.MEDIA_MOUNTED)) {
  7. try {
  8. //Get the file directory for the extended storage device
  9. File Sdfile = android.os.Environment
  10. . getExternalStorageDirectory ();
  11. //Open File
  12. File MyFile = new File (Sdfile.getabsolutepath ()
  13. + File.separator + "MyFile.txt");
  14. //Determine if it exists, does not exist then creates
  15. if (!myfile.exists ()) {
  16. Myfile.createnewfile ();
  17. }
  18. //Write Data
  19. String Szouttext = "Hello, world!";
  20. FileOutputStream outputstream = new FileOutputStream (MyFile);
  21. Outputstream.write (Szouttext.getbytes ());
  22. Outputstream.close ();
  23. } catch (Exception e) {
  24. //Todo:handle exception
  25. }//end of Try
  26. }//End of If (media_mounted)
  27. Have read-only permissions
  28. else if (sdstatestring
  29. . EndsWith (Android.os.Environment.MEDIA_MOUNTED_READ_ONLY)) {
  30. //Get the file directory for the extended storage device
  31. File sdfile = Android.os.Environment.getExternalStorageDirectory ();
  32. //Create a file
  33. File MyFile = new File (Sdfile.getabsolutepath () + File.separator
  34. + "MyFile.txt");
  35. //Determine if the file exists
  36. if (myfile.exists ()) {
  37. try {
  38. //Read Data
  39. FileInputStream InputStream = new FileInputStream (MyFile);
  40. byte[] buffer = new byte[1024];
  41. Inputstream.read (buffer);
  42. Inputstream.close ();
  43. } catch (Exception e) {
  44. //Todo:handle exception
  45. }//end of Try
  46. }//End of If (MyFile)
  47. }//End of If (media_mounted_read_only)
  48. 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)

  1. public void Sdcardsizetest () {
  2. Get the current state of SDcard
  3. String sdcstring = Android.os.Environment.getExternalStorageState ();
  4. if (Sdcstring.equals (Android.os.Environment.MEDIA_MOUNTED)) {
  5. //Get sdcard file path
  6. File Pathfile = android.os.Environment
  7. . getExternalStorageDirectory ();
  8. Android.os.StatFs StatFs = new Android.os.StatFs (Pathfile.getpath ());
  9. //Get total block on SDcard
  10. Long ntotalblocks = Statfs.getblockcount ();
  11. //Get the size of each block on the SDcard
  12. Long nblocsize = Statfs.getblocksize ();
  13. //Get the number of blocks available for the program to use
  14. Long Navailablock = Statfs.getavailableblocks ();
  15. //Get the number of all remaining blocks (including block that is not available for General program reservation)
  16. Long Nfreeblock = Statfs.getfreeblocks ();
  17. //Calculate SDcard Total capacity MB
  18. Long nsdtotalsize = ntotalblocks * Nblocsize/ 1024x768/ 1024;
  19. //Calculate sdcard remaining size MB
  20. Long nsdfreesize = Navailablock * Nblocsize/ 1024x768/ 1024;
  21. }//end of If
  22. End of Func
Reprinted from: http://crackren.javaeye.com/blog/747121

"Go" Android sdcard operation (file read/write, capacity calculation)

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.