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 }