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 );}