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
- <!--Create and delete files in SDcard permissions--
- <uses-permission android:name="Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
- <!--write data to sdcard permissions--
- <uses-permission android:name="Android.permission.WRITE_EXTERNAL_STORAGE"/>
Does the SD card exist
[Java]View Plaincopy
- Private Boolean Existsdcard () {
- if (Android.os.Environment.getExternalStorageState (). Equals (
- Android.os.Environment.MEDIA_MOUNTED)) {
- return true;
- } Else
- return false;
- }
SD Card remaining space
[Java]View Plaincopy
- Public Long Getsdfreesize () {
- //Get SD card file path
- File path = Environment.getexternalstoragedirectory ();
- StatFs SF = new StatFs (Path.getpath ());
- //Gets the size of a single data block (Byte)
- Long blockSize = Sf.getblocksize ();
- //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)/1024x768/1024; //units MB
- }
Total SD card capacity
[Java]View Plaincopy
- Public Long Getsdallsize () {
- //Get SD card file path
- File path = Environment.getexternalstoragedirectory ();
- StatFs SF = new StatFs (Path.getpath ());
- //Gets the size of a single data block (Byte)
- Long blockSize = Sf.getblocksize ();
- //Get 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; //units MB
- }