To display the current sdcard capacity through progressbar.
Code:
Activity Code:
Package cn.com. chenzheng_java; </P> <p> Import Java. io. file; </P> <p> Import android. app. activity; <br/> Import android. OS. bundle; <br/> Import android. OS. environment; <br/> Import android. OS. statfs; <br/> Import android. util. log; <br/> Import android. view. view; <br/> Import android. view. view. onclicklistener; <br/> Import android. widget. button; <br/> Import android. widget. progressbar; <br/> Import android. widget. toast; </P> <p> public class sdcardactivity extends activity implements onclicklistener {</P> <p> string result = "sdcard capacity information:/N "; <br/> progressbar; <br/> button; </P> <p> @ override <br/> protected void oncreate (bundle savedinstancestate) {<br/> super. oncreate (savedinstancestate); <br/> setcontentview (R. layout. sdcard); <br/> progressbar = (progressbar) findviewbyid (R. id. progressbar_sdcard); <br/> button = (button) findviewbyid (R. id. button_sdcard); <br/> button. setonclicklistener (this); </P> <p >}</P> <p> private void showsdcardsize () {<br/> progressbar. setprogress (0); </P> <p> file sdcard = environment. getexternalstoragedirectory (); <br/>/** <br/> * You can use statfs to access the space capacity of the file system. <br/> */<br/> statfs = new statfs (sdcard. getpath (); </P> <p>/** <br/> * statfs. getblocksize: obtains the number of bytes occupied by a block in the current file system. <br/> */<br/> int blocksize = statfs. getblocksize (); <br/>/** <br/> * statfs. the getavaliableblocks method returns the number of unused blocks <br/> */<br/> int avaliableblocks = statfs. getavailableblocks (); <br/>/** <br/> * statfs. getblockcount can obtain the total number of blocks <br/> */<br/> int totalblocks = statfs. getblockcount (); </P> <p> result + = "/n space not used:" + avaliableblocks * blocksize + "Byte "; </P> <p> result + = "/n total space size:" + totalblocks * blocksize + "Byte"; <br/> float a = (float) avaliableblocks/totalblocks; <br/> int B = integer. valueof (float. valueof (A * 100 ). tostring (). substring (0, 2); </P> <p> progressbar. setprogress (90); <br/> log. I ("notification", result); <br/> toast. maketext (this, B + "" + result, toast. length_long ). show (); </P> <p >}</P> <p> @ override <br/> Public void onclick (view V) {<br/> showsdcardsize (); </P> <p >}< br/>
Sdcard. xml
<? XML version = "1.0" encoding = "UTF-8"?> <Br/> <linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" <br/> Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" <br/> Android: orientation = "vertical"> <br/> <! -- <Br/> set progressbar to a horizontal long box instead of a small circle that is always rotated. Use the following statement to set <br/> style = "? Android: ATTR/progressbarstylehorizontal "<br/> or <br/> style = "? Android: progressbarstylehorizontal "<br/> --> <br/> <progressbar Android: Id =" @ + ID/progressbar_sdcard "<br/> Android: progress = "0" style = "? Android: progressbarstylehorizontal "mce_style = "? Android: progressbarstylehorizontal "<br/> Android: max =" 100 "<br/> Android: layout_width =" fill_parent "Android: layout_height = "wrap_content"> </progressbar> <br/> <button Android: Id = "@ + ID/button_sdcard" Android: layout_width = "wrap_content" <br/> Android: TEXT = "button" Android: layout_height = "wrap_content"> </button> <br/> </linearlayout> <br/>
All other files are default.
Bytes --------------------------------------------------------------------------------------------
Here, we only need to use a class android. OS. statfs, which allows us to access the capacity-related information of the file system.
The main methods are as follows: