/** * Get common path */public void Getdir () {///mnt/sdcardfile root=environment.getexternalstoragedirectory ();///systemFile Root=environment.getrootdirectory ();///cachefile root=environment.getdownloadcachedirectory ();///dataFile root= Environment.getdatadirectory ();///storage/emulated/0/music//string type=environment.directory_music;//string Type=environment.directory_pictures; File root=environment.getexternalstoragepublicdirectory (type); System.out.println ("root=" +root.getabsolutepath ());} /** * Get the total capacity of the phone SDcard * @return */public long getsdcardtotalcapacity () {if (This.issdcarduse ()) {///mnt/sdcardfile root= Environment.getexternalstoragedirectory ();//Get state File System Object StatFs statfs=new StatFs (Root.getpath ()) according to SDcard path;// Get the size of each piece of data, in bytes long blocksize=statfs.getblocksize (); System.out.println ("blocksize=" +blocksize);//Get the current phone sdcard altogether how many blocks of data long blockcount=statfs.getblockcount (); System.out.println ("blockcount=" +blockcount);//return blockcount*blocksize;//default in bytes return blockcount*blocksize /1024/1024/1024;//Default in bytes, converted to Gb}else{throw new RuntimeException ("Current phone sdcard does not exist or is not available!") ");}} /** * Get mobile sdcard usable capacity * @return */public long getsdcardavailablecapacity () {if (This.issdcarduse ()) {///mnt/sdcardfile Root=environment.getexternalstoragedirectory ();//SDcard The state file system object according to the path of the StatFs statfs=new statFs ( Root.getabsolutepath ());//Get the size of each piece of data, in bytes int blocksize=statfs.getblocksize ();//Get the current phone sdcard usable capacity of SDcard block number int Availableblocks=statfs.getavailableblocks ();//return availableblocks*blocksize;//default in bytes return availableBlocks* blocksize/1024/1024/1024;//Default in bytes, converted to Gb}else{throw new RuntimeException ("Current phone sdcard does not exist or is not available!") ");}} /** * Get phone sdcard remaining capacity * @return */public long getsdcardfreecapacity () {if (This.issdcarduse ()) {///mnt/sdcardfile root= Environment.getexternalstoragedirectory ();//Get state File System Object StatFs statfs=new StatFs (Root.getabsolutepath () according to SDcard Path );//Get the size of each piece of data, in bytes int blocksize=statfs.getblocksize ();//Get the current cell phone sdcard usable capacity sdcard block number int freeblocks= Statfs.getfreeblocks ();//return freeblocks*blocksize;//default in bytes reTurn freeblocks*blocksize/1024/1024/1024;//Default in bytes, converted to Mb}else{throw new RuntimeException ("Current phone sdcard does not exist or is not available!") ");}}}
Operation of the Android SD card