In order to have βfreeβ space on the external SD card, to display a number that is consistent with the menu-> Settings-> SD card and phone number, use the following code:
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath()); double sdAvailSize = (double)stat.getAvailableBlocks() * (double)stat.getBlockSize();
Here's how you get the internal storage sizes:
StatFs statFs = new StatFs(Environment.getRootDirectory().getAbsolutePath()); long blockSize = statFs.getBlockSize(); long totalSize = statFs.getBlockCount()*blockSize; long availableSize = statFs.getAvailableBlocks()*blockSize; long freeSize = statFs.getFreeBlocks()*blockSize;
Here's how you get the external storage sizes (SD card size):
StatFs statFs = new StatFs(Environment.getExternalStorageDirectory().getAbsolutePath()); long blockSize = statFs.getBlockSize(); long totalSize = statFs.getBlockCount()*blockSize; long availableSize = statFs.getAvailableBlocks()*blockSize; long freeSize = statFs.getFreeBlocks()*blockSize;
Brief note
Free blocks:
The total number of blocks that are free on the file system, including reserved blocks (which are not available for regular applications).
Available blocks:
The number of blocks free from the file system and available to the application.
Here's how to determine if an SD card is installed:
String state = Environment.getExternalStorageState(); if (Environment.MEDIA_MOUNTED.equals(state)) { // We can read and write the media } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { // We can only read the media } else { // No external media }
Related documentation: http://developer.android.com/reference/android/os/StatFs.html
MDMalik May 30 '13 at 11:40 2013-05-30 11:40
source share