Android Get sdcard Info

Source: Internet
Author: User

<LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Xmlns:tools= "Http://schemas.android.com/tools"Android:id= "@+id/linearlayout1"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Tools:context=". Mainactivity " >    <!--Label control showing the number of blocks -    <TextViewAndroid:id= "@+id/tv_totalblocks"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" />    <!--Label control showing the size of the block -    <TextViewAndroid:id= "@+id/tv_blocsize"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" />    <!--to display the label control for the available block -    <TextViewAndroid:id= "@+id/tv_availablock"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" />    <!--display a Label control for an empty block -    <TextViewAndroid:id= "@+id/tv_freeblock"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" />    <!--Label control showing the total size of the SDcard -    <TextViewAndroid:id= "@+id/tv_sdtotalsize"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" />    <!--Label control showing the remaining size of the SDcard -    <TextViewAndroid:id= "@+id/tv_sdfreesize"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content" /></LinearLayout>
 Packagecom.example.yanlei.yl2;ImportAndroid.os.Bundle;Importandroid.support.v7.app.AppCompatActivity;ImportAndroid.widget.TextView;ImportJava.io.File; Public classMainactivityextendsappcompatactivity {PrivateTextView mtvtotalblocks;//sdcard number of blocks on the block    PrivateTextView mtvblocsize;//size of each block on the SDcard    PrivateTextView Mtvavailablock;//number of blocks available for the program to use    PrivateTextView Mtvfreeblock;//The number of all remaining blocks (including chunks that are not available in the General program reserved)    PrivateTextView mtvsdtotalsize;//sdcard Total capacity size MB    PrivateTextView mtvsdfreesize;//sdcard remaining size MB@Override Public voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); //get the controls in the layoutFindview (); //Binding Control Eventssdcardsizetest (); }    Private voidFindview () {//Bound ControlMtvtotalblocks =(TextView) Findviewbyid (r.id.tv_totalblocks); Mtvblocsize=(TextView) Findviewbyid (r.id.tv_blocsize); Mtvavailablock=(TextView) Findviewbyid (R.id.tv_availablock); Mtvfreeblock=(TextView) Findviewbyid (R.id.tv_freeblock); Mtvsdtotalsize=(TextView) Findviewbyid (r.id.tv_sdtotalsize); Mtvsdfreesize=(TextView) Findviewbyid (r.id.tv_sdfreesize); }     Public voidsdcardsizetest () {//get the current state of SDcardString sdcstring =android.os.Environment.getExternalStorageState (); //if the current system has sdcard present        if(Sdcstring.equals (Android.os.Environment.MEDIA_MOUNTED)) {//get sdcard file pathFile Pathfile =android.os.Environment. getExternalStorageDirectory (); //get the state of SDcardAndroid.os.StatFs StatFs =NewAndroid.os.StatFs (Pathfile.getpath ()); //get total block on SDcard            LongNtotalblocks =Statfs.getblockcount (); Mtvtotalblocks.settext ("Total blocks on SDcard:" +ntotalblocks); //gets the size of each block on the SDcard            LongNblocsize =statfs.getblocksize (); Mtvblocsize.settext ("Size of each bloc on the sdcard:" +nblocsize); //gets the number of blocks available for the program to use            LongNavailablock =statfs.getavailableblocks (); Mtvavailablock.settext ("Number of blocks available for program use:" +Navailablock); //get the number of all remaining blocks (including chunks that are not available for General program reservation)            LongNfreeblock =statfs.getfreeblocks (); Mtvfreeblock.settext ("The number of all remaining blocks:" +Nfreeblock); //calculate sdcard Total capacity MB            LongNsdtotalsize = Ntotalblocks * nblocsize/1024/1024; Mtvsdtotalsize.settext ("SDcard Total capacity size MB:" + nsdtotalsize + "MB"); //calculate sdcard remaining size MB            LongNsdfreesize = Navailablock * nblocsize/1024/1024; Mtvsdfreesize.settext ("SDcard Remaining size MB:" + nsdfreesize + "MB"); }    }}

Android Get sdcard Info

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.