Android study notes -- getting SD card and mobile ROM capacity

Source: Internet
Author: User

Continue the android learning journey. Today, we use a small example to get the SD card capacity and the phone ROM capacity. The Code is as follows:

Package com. urovo. sdcardspace; import java. io. file; 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 {@ Overrideprotected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); TextView TV = (TextView) findViewById (R. id. TV); File path = Environment. getExternalStorageDirectory (); // get the path of the SD card StatFs stat = new StatFs (path. getPath (); // create a StatFs object to obtain the file system status long blockCount = stat. getBlockCount (); long blockSize = stat. getBlockSize (); long availableBlocks = stat. getAvailableBlocks (); String totalSize = Formatter. formatFileSize (getApplicationContext (), blockCount * blockSize); // format the total size of the SD card String availableSize = Formatter. formatFileSize (getApplicationContext (), blockCount * availableBlocks); // get the available capacity of the SD card TV. setText ("Total SD card capacity:" + totalSize + "\ nSD card available capacity:" + availableSize + "\ n" + getRomSpace ();} private String getRomSpace () {File path = Environment. getDataDirectory (); StatFs stat = new StatFs (path. getPath (); long blockCount = stat. getBlockCount (); long blockSize = stat. getBlockSize (); long availableBlocks = stat. getAvailableBlocks (); String totalSize = Formatter. formatFileSize (getApplicationContext (), blockCount * blockSize); String availableSize = Formatter. formatFileSize (getApplicationContext (), blockCount * availableBlocks); return "total phone Rom capacity:" + totalSize + "\ n phone Rom available capacity:" + availableSize ;}}

The specific content can be further understood by analyzing the source code of android settings.

 

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.