Android to determine if SD card exists and capacity query

Source: Internet
Author: User

The first step is to increase the access rights of the SD card in Androidmanifest.xml

<!--Create and delete files in SDcard permissions--  <uses-permission android:name= "android.permission.MOUNT_UNMOUNT_ Filesystems "/>  <!--writing data to SDcard permissions--  <uses-permission android:name=" Android.permission.WRITE _external_storage "/>   

does the SD card exist

Private Boolean Existsdcard () {    if (android.os.Environment.getExternalStorageState (). Equals (      Android.os.Environment.MEDIA_MOUNTED) {     return true;    } else     return false;   }  

SD Card remaining space

Public long Getsdfreesize () {       //Get SD card file path       = Environment.getexternalstoragedirectory ();        StatFs SF = new StatFs (Path.getpath ());        Gets the size of a single data block (Byte)       Long blockSize = Sf.getblocksize ();        The number of idle data blocks       long freeblocks = Sf.getavailableblocks ();       Return SD card idle size       //return freeblocks * blockSize;  Unit byte       //return (Freeblocks * blockSize)/1024;   Unit KB       return (freeblocks * blockSize)/1024/1024;//unit MB     }   
total SD card capacity

Public long Getsdallsize () {       //Get SD card file path       = Environment.getexternalstoragedirectory ();        StatFs SF = new StatFs (Path.getpath ());        Gets the size of a single data block (Byte)       Long blockSize = Sf.getblocksize ();        Gets the number of all data blocks       long allblocks = Sf.getblockcount ();       Return SD card size       //return allblocks * blockSize;//Unit byte       //return (Allblocks * blockSize)/1024;//unit KB       return ( Allblocks * blockSize)/1024/1024; Unit MB     }   




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.