Android read SD card capacity

Source: Internet
Author: User

In the development, we often use the SD card, then when the SD card read and write, we often need to determine whether the remaining capacity of the SD card is sufficient. So this time we're going to write a program that gets the SD card capacity.

The attention of the place, I have in the program has been noted. See the program basically understand ha.

Let's take a look at the results of the operation.

Layout file Activity_main.xml

<LinearLayoutxmlns:android= "Http://"Xmlns:tools= "Http://"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"Android:paddingbottom= "@dimen/activity_vertical_margin"Android:paddingleft= "@dimen/activity_horizontal_margin"Android:paddingright= "@dimen/activity_horizontal_margin"Android:paddingtop= "@dimen/activity_vertical_margin"Tools:context= "Com.example.sdcard.MainActivity" >    <TextViewAndroid:id= "@+id/tv"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" />    <TextViewAndroid:id= "@+id/tv2"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" />          <TextViewAndroid:id= "@+id/tv3"Android:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "@string/hello_world" /></LinearLayout>

Java file

 PackageCom.example.sdcard;;ImportAndroid.annotation.SuppressLint;;ImportAndroid.os.Build;ImportAndroid.os.Bundle;Importandroid.os.Environment;ImportAndroid.os.StatFs;ImportAndroid.text.format.Formatter;ImportAndroid.widget.TextView;Importandroid.widget.Toast; @SuppressLint ("Showtoast") Public classMainactivtyextendsActivity {@SuppressWarnings ("Deprecation") @SuppressLint ("Newapi") @Overrideprotected voidonCreate (Bundle savedinstancestate) {//TODO auto-generated Method Stub        Super. OnCreate (savedinstancestate);        Setcontentview (R.layout.activity_main); TextView TV=(TextView) Findviewbyid (; TextView TV2=(TextView) Findviewbyid (R.ID.TV2); TextView TV3=(TextView) Findviewbyid (R.ID.TV3); LongblockSize; Longtotalblocks; Longavaibleblocks; //determine if a memory card is plugged in and mounted        if(Environment.getexternalstoragestate (). Equals (environment.media_mounted)) {File path =environment.getexternalstoragedirectory (); StatFs StatFs=NewStatFs (Path.getpath ()); /** Build.VERSION.SDK_INT: Gets the current system version level * Build.version_codes. JELLY_BEAN_MR2 that Android 4.3, that is 18, here directly write 18 can also * because Getblocksizelong () and other three methods are only after the Android 4.3, so it is necessary to determine the current system version * Supplement One             A knowledge point: All storage devices are divided into blocks, each with a fixed size. */            if(Build.VERSION.SDK_INT >=Build.version_codes. JELLY_BEAN_MR2) {//get the number of blocksBlockSize =Statfs.getblocksizelong (); //get a total of how many blocksTotalblocks =Statfs.getblockcountlong (); //blocks that can be activeAvaibleblocks =Statfs.getavailableblockslong (); Tv.settext ("Total space:" + formatsize (totalblocks *blockSize)); Tv2.settext ("Free space:" + formatsize (avaibleblocks *blockSize)); Tv3.settext ("Used space:" + formatsize (totalblocks *blockSize)-(Avaibleblocks *( blockSize))); } Else {                /** The black line indicates that the three APIs are obsolete. But in order to be compatible with 4.3 systems, we need to write*/blockSize=statfs.getblocksize (); Totalblocks=Statfs.getblockcount (); Avaibleblocks=statfs.getavailableblocks (); Tv.settext (Formatsize (avaibleblocks*blockSize)); }        } Else{Toast.maketext ( This, "SD card not found", 0); }    }    PrivateString Formatsize (Longsize) {        //formats the displayed data.         returnFormatter.formatfilesize (mainactivty. This, size); }}

Android read SD card capacity

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: 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.