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

Source: Internet
Author: User

 

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 ---------------------------------------------------------------------------------------------------------

  1. // Sdcard operation
  2. Ublic void sdcardtest (){
  3. // Get the status of the extended SD card device
  4. String sdstatestring = Android. OS. environment. getexternalstoragestate ();
  5. // Read and Write Permissions
  6. If (sdstatestring. Equals (Android. OS. environment. media_mounted )){
  7. Try {
  8. // Obtain the file directory of the extended storage device
  9. File sdfile = Android. OS. Environment
  10. . Getexternalstoragedirectory ();
  11. // Open the file
  12. File myfile = new file (sdfile. getabsolutepath ()
  13. + File. Separator + "myfile.txt ");
  14. // Determine whether the object exists. If the object does not exist, the object is created.
  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. // Read-only permission
  28. Else if (sdstatestring
  29. . Endswith (Android. OS. environment. media_mounted_read_only )){
  30. // Obtain the file directory of the extended storage device
  31. File sdfile = Android. OS. environment. getexternalstoragedirectory ();
  32. // Create an object
  33. File myfile = new file (sdfile. getabsolutepath () + file. Separator
  34. + "Myfile.txt ");
  35. // Determine whether a 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
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 

 

Related Article

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.