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.