Android: Getting internal and external memory capacity

Source: Internet
Author: User

First, let's take a look at the knowledge about memory cards:

Before using the new disk or SD card, to make the operating system recognize it, you must first write some magnetic marks to each sector on the disk, you can use the data on the disk under the Operating System. This process is formatted.

Formatting can be straightforward as dividing your disk or SD card into several blocks before you can store data in it. Each file occupies a different "cell. Data cannot be stored without formatting. Currently, the SD card has been formatted and can be used directly.

In a small experiment, you can create a text file in a Windows system and write a character into it to check that the attribute size is 1 byte, however, the occupied space is 4 kb (4096 bytes). Only when your storage content exceeds 4 kb, the occupied space of the file changes to 8 KB, the size of a file's storage block is 4 kb.

 


Now let's take a look at how to get the size of the android internal and external memory through code:

 

Protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_main); externalTv = (TextView) findViewById (R. id. tv1); internalTv = (TextView) findViewById (R. id. tv2); // first check the status of the SD card, whether it is mounted or removed if (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED) {// get the file directory String path = Environment of the SD card (external storage device, not necessarily the SD card. getExternalStorageDirectory (). getPath (); // a packaging class used to retrieve the file system information StatFs stat = new StatFs (path); // the size of the file system block (byte) long blockSize = stat. getBlockSize (); // The total number of blocks in the file system long totalBlocks = stat. getBlockCount (); // Number of idle storage blocks on the file system that can be used for programs long availableBlocks = stat. getAvailableBlocks (); // total capacity long totalSize = blockSize * totalBlocks; long availableSize = blockSize * availableBlocks; String totalStr = Formatter. formatFileSize (this, totalSize); String availableStr = Formatter. formatFileSize (this, availableSize); externalTv. setText ("External Storage Capacity" + "\ n" + "total size:" + totalStr + "\ n" + "available size:" + availableStr );} else {Toast. makeText (this, "SD card unavailable, please check the SD card status", Toast. LENGTH_LONG ). show ();} File path = Environment. getDataDirectory (); StatFs stat = new StatFs (path. getPath (); // The Block Size of the file system (byte) long blockSize1 = stat. getBlockSize (); // The total number of file system blocks long totalBlocks1 = stat. getBlockCount (); // Number of idle storage blocks on the file system that can be used for programs long availableBlocks1 = stat. getAvailableBlocks (); // total capacity long totalSize1 = blockSize1 * totalBlocks1; long availableSize1 = blockSize1 * availableBlocks1; String totalStr1 = Formatter. formatFileSize (this, totalSize1); String availableStr1 = Formatter. formatFileSize (this, availableSize1); internalTv. setText ("internal storage size" + "\ n" + "total size:" + totalStr1 + "\ n" + "available size:" + availableStr1 );}

 

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.