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 SD card status, whether it is mounted or removed
If (Environment. getExternalStorageState (). equals (Environment. MEDIA_MOUNTED )){
// Get the directory of the SD card (external storage device, not necessarily the SD card)
String path = Environment. getExternalStorageDirectory (). getPath ();
// A packaging class used to retrieve information about a file system
StatFs stat = new StatFs (path );
// File system block size (byte)
Long blockSize = stat. getBlockSize ();
// Total number of file system blocks
Long totalBlocks = stat. getBlockCount ();
// Number of idle storage blocks that can be used for programs on the file system
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,
"The SD card is unavailable. Check the SD card status ",
Toast. LENGTH_LONG). show ();
}
File path = Environment. getDataDirectory ();
StatFs stat = new StatFs (path. getPath ());
// File system block size (byte)
Long blockSize1 = stat. getBlockSize ();
// Total number of file system blocks
Long totalBlocks1 = stat. getBlockCount ();
// Number of idle storage blocks that can be used for programs on the file system
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 ("size of internal storage" + "n" + "total size:" + totalStr1 + "n" + "available size:" + availableStr1 );
}