Android obtains the SDCard information, androidsdcard
<LinearLayout xmlns: 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 "> <! -- Display the tag control of the number of blocks --> <TextView android: id = "@ + id/TV _TotalBlocks" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <! -- Display the label control of the block size --> <TextView android: id = "@ + id/TV _BlocSize" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <! -- Display the tag control of available blocks --> <TextView android: id = "@ + id/TV _AvailaBlock" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <! -- Display the label control of an empty block --> <TextView android: id = "@ + id/TV _FreeBlock" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <! -- Display the tag control of the total size of SDCard --> <TextView android: id = "@ + id/TV _SDTotalSize" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> <! -- Display the remaining tag control of SDCard --> <TextView android: id = "@ + id/TV _SDFreeSize" android: layout_width = "wrap_content" android: layout_height = "wrap_content"/> </LinearLayout>
Package com. example. yanlei. yl2; import android. OS. bundle; import android. support. v7.app. appCompatActivity; import android. widget. textView; import java. io. file; public class MainActivity extends AppCompatActivity {private TextView mTvTotalBlocks; // total number of blocks on the SDCard private TextView mTvBlocSize; // the SIZE of each BLOCK on the SDCard private TextView mTvAvailaBlock; // The number of blocks available for the program private TextView mTvFreeBlock; // The number of all blocks remaining (including the blocks not available for the reserved General Program) private TextView mTvSDTotalSize; // total size of SDCard MB private TextView mTvSDFreeSize; // remaining size of SDCard MB @ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); // obtain the findView () control in the layout; // bind the control event SDCardSizeTest ();} private void findView () {// bind the control mTvTotalBlocks = (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 void SDCardSizeTest () {// get the current status of SDCard String sDcString = android. OS. environment. getExternalStorageState (); // if the current system has sdcard, if (sDcString. equals (android. OS. environment. MEDIA_MOUNTED) {// get the sdcard File path File pathFile = android. OS. environment. getExternalStorageDirectory (); // get the sdcard status android. OS. statFs statfs = new android. OS. statFs (pathFile. getPath (); // obtain the total number of blocks on the SDCard. long nTotalBlocks = statfs. getBlockCount (); mTvTotalBlocks. setText ("Total number of blocks on SDCard:" + nTotalBlocks); // obtain the SIZE of each BLOCK on SDCard long nBlocSize = statfs. getBlockSize (); mTvBlocSize. setText ("SIZE of each bloc on SDCard:" + nBlocSize); // obtain the number of blocks available for the program. long nAvailaBlock = statfs. getAvailableBlocks (); mTvAvailaBlock. setText ("number of blocks available for the Program:" + nAvailaBlock); // obtain the number of all remaining blocks (including blocks that are unavailable to the general program) long nFreeBlock = statfs. getFreeBlocks (); mTvFreeBlock. setText ("Number of all remaining blocks:" + nFreeBlock); // calculate the total capacity of the SDCard (MB long nSDTotalSize = nTotalBlocks * nBlocSize/1024/1024; mTvSDTotalSize. setText ("Total SDCard capacity MB:" + nSDTotalSize + "MB"); // calculate the remaining SDCard size MB long nSDFreeSize = nAvailaBlock * nBlocSize/1024/1024; mTvSDFreeSize. setText ("remaining SDCard size MB:" + nSDFreeSize + "MB ");}}}