Android to determine if SD card exists and capacity query

Source: Internet
Author: User

An easy way for Android to determine the presence and capacity of an SD card is as follows:
The first step is to increase the access rights of the SD card in Androidmanifest.xml

[HTML]View Plaincopy
    1. <!--Create and delete files in SDcard permissions--
    2. <uses-permission android:name="Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
    3. <!--write data to sdcard permissions--
    4. <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>

Does the SD card exist

[Java]View Plaincopy
    1. Private Boolean Existsdcard () {
    2. if (Android.os.Environment.getExternalStorageState (). Equals (
    3. Android.os.Environment.MEDIA_MOUNTED)) {
    4. return true;
    5. } Else
    6. return false;
    7. }
SD Card remaining space [Java]View Plaincopy
  1. Public Long Getsdfreesize () {
  2. //Get SD card file path
  3. File path = Environment.getexternalstoragedirectory ();
  4. StatFs SF = new StatFs (Path.getpath ());
  5. //Gets the size of a single data block (Byte)
  6. Long blockSize = Sf.getblocksize ();
  7. //number of idle data blocks
  8. Long freeblocks = Sf.getavailableblocks ();
  9. //Return SD card idle size
  10. //return freeblocks * blockSize; Unit byte
  11. //return (freeblocks * blockSize)/1024; Unit KB
  12. return (Freeblocks * blockSize)/1024x768/1024; //units MB
  13. }
Total SD card capacity [Java]View Plaincopy
  1. Public Long Getsdallsize () {
  2. //Get SD card file path
  3. File path = Environment.getexternalstoragedirectory ();
  4. StatFs SF = new StatFs (Path.getpath ());
  5. //Gets the size of a single data block (Byte)
  6. Long blockSize = Sf.getblocksize ();
  7. //Get the number of all data blocks
  8. Long allblocks = Sf.getblockcount ();
  9. //Return SD card size
  10. //return allblocks * blockSize;//Unit byte
  11. //return (allblocks * blockSize)/1024;//unit KB
  12. return (Allblocks * blockSize)/1024/1024; //units MB
  13. }




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.