Android storage operation Size Display converts kb to MB read

Source: Internet
Author: User
Package IRDC. cunchu; import IRDC. cunchu. r; import Java. io. file; import Java. text. decimalformat; import android. app. activity; import android. OS. bundle; import android. OS. environment; import android. OS. statfs; import android. view. view; import android. widget. button; import android. widget. progressbar; import android. widget. textview; public class cunchu extends activity {private button mybutton; private progressbar myprogressbar; private textview mytextview;/** called when the activity is first created. * // @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); mybutton = (button) findviewbyid (R. id. mybutton); myprogressbar = (progressbar) findviewbyid (R. id. myprogressbar); mytextview = (textview) findviewbyid (R. id. mytextview); mybutton. setonclicklistener (New button. onclicklistener () {@ override public void onclick (view arg0) {showsize () ;}}) ;} private void showsize () {/* Set textview and progressbar to null and 0 */mytextview. settext (""); myprogressbar. setprogress (0);/* determine whether the memory card is inserted */If (environment. getexternalstoragestate (). equals (environment. media_mounted) {/*. The path to the SD card file is/sdcard */File Path = environment. getexternalstoragedirectory ();/* statfs View File System space usage */statfs = new statfs (path. getpath ();/* block size */long blocksize = statfs. getblocksize ();/* Total number of blocks */long totalblocks = statfs. getblockcount ();/* number of blocks in use */long availableblocks = statfs. getavailableblocks (); string [] Total = filesize (totalblocks * blocksize); string [] available = filesize (availableblocks * blocksize);/* getmax is obtained in main. maximum value set by progressbar in XML */INT Ss = integer. parseint (available [0]) * myprogressbar. getmax ()/integer. parseint (total [0]); myprogressbar. setprogress (SS); string text = "Total" + total [0] + total [1] + "\ n "; text + = "available" + available [0] + available [1]; mytextview. settext (text);} else if (environment. getexternalstoragestate (). equals (environment. media_removed) {string text = "SD card deleted"; mytextview. settext (text) ;}/ * returns the string array [0] As the size [1] In kb or MB */private string [] filesize (Long SIZE) {string STR = ""; if (size> = 1024) {STR = "kb"; size/= 1024; If (size> = 1024) {STR = "MB "; size/= 1024 ;}} decimalformat formatter = new decimalformat ();/* separate three numbers, for example, 1,000 */formatter. setgroupingsize (3); string result [] = new string [2]; Result [0] = formatter. format (size); Result [1] = STR; return result ;}}

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="irdc.cunchu"      android:versionCode="1"      android:versionName="1.0.0">    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name="irdc.cunchu.cunchu"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>    </application></manifest> 

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  android:orientation="vertical"  android:layout_width="fill_parent"  android:layout_height="fill_parent"  >  <Button  android:id="@+id/myButton"  android:layout_width="wrap_content"  android:layout_height="wrap_content"    android:text="@string/strButton"  >  </Button>  <TextView    android:id="@+id/myTextView"  android:layout_width="fill_parent"   android:layout_height="wrap_content"   >  </TextView>  <ProgressBar   android:id="@+id/myProgressBar"  style="?android:attr/progressBarStyleHorizontal"  android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:max="100"  android:progress="0">  </ProgressBar></LinearLayout>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.