Android_02 _ Get the available capacity of the SD card

Source: Internet
Author: User

Android_02 _ Get the available capacity of the SD card

The sample code is as follows:

 

Package com. itheima. getsdavail; import java. io. file; import android. OS. build; import android. OS. bundle; import android. OS. environment; import android. OS. statFs; import android. app. activity; import android. text. format. formatter; import android. view. menu; import android. widget. textView; public class MainActivity extends Activity {@ SuppressWarnings (deprecation) @ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); File path = Environment. getExternalStorageDirectory (); StatFs stat = new StatFs (path. getPath (); long blockSize; long totalBlocks; long availableBlocks; // obtain the level of the current system version if (Build. VERSION. SDK_INT> = Build. VERSION_CODES.JELLY_BEAN_MR2 ){
// Later versions of Android support the following operations: blockSize = stat. getBlockSizeLong (); totalBlocks = stat. getBlockCountLong (); availableBlocks = stat. getAvailableBlocksLong ();}
// Earlier Android versions support the following else {blockSize = stat. getBlockSize (); totalBlocks = stat. getBlockCount (); availableBlocks = stat. getAvailableBlocks ();} TextView TV = (TextView) findViewById (R. id. TV); TV. setText (formatSize (availableBlocks * blockSize);} private String formatSize (long size) {return Formatter. formatFileSize (this, size );}}

 

 

Note: You can use the Setting application of the source code to view how the app gets the remaining space of the SD card, so that we can get the remaining capacity of the SD card through reference.

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.