ImportJava.io.File;ImportAndroid.os.Build;ImportAndroid.os.Bundle;Importandroid.os.Environment;ImportAndroid.os.StatFs;Importandroid.app.Activity;ImportAndroid.text.format.Formatter;ImportAndroid.view.Menu;ImportAndroid.widget.TextView; Public classMainactivityextendsActivity {@Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); File Path=environment.getexternalstoragedirectory (); StatFs Stat=NewStatFs (Path.getpath ()); LongblockSize; Longtotalblocks; Longavailableblocks; //Detection System Current version number if(Build.VERSION.SDK_INT >=Build.version_codes. JELLY_BEAN_MR2) {blockSize=Stat.getblocksizelong (); Totalblocks=Stat.getblockcountlong (); Availableblocks=Stat.getavailableblockslong (); } Else{blockSize=stat.getblocksize (); Totalblocks=Stat.getblockcount (); Availableblocks=stat.getavailableblocks (); } TextView TV=(TextView) Findviewbyid (r.id.tv); Tv.settext (Formatsize (availableblocks*blockSize)); } PrivateString Formatsize (Longsize) { returnFormatter.formatfilesize ( This, size); }}
Android Basics-Get SD card available capacity