要获得外部SD卡可用的“免费”空间以显示与菜单 – >设置 – > SD卡和手机存储号码一致的号码,请使用以下代码:
StatFs stat = new StatFs(Environment.getExternalStorageDirectory().getPath());
double sdAvailSize = (double)stat.getAvailableBlocks()
* (double)stat.getBlockSize();
//One binary gigabyte equals 1,073,741,824 bytes.
double gigaAvailable = sdAvailSize / 1073741824;
以下是获取内部存储大小的方法:
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;
以下是外部存储容量(SD卡大小)的方法:
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;
简短说明
免费区块:
The total number of blocks that are
free on the file system, including
reserved blocks (that are not
available to normal applications).
可用块:
The number of blocks that are free on
the file system and available to
applications.
以下是检测SD卡是否已安装的方法:
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
}
相关文件:http://developer.android.com/reference/android/os/StatFs.html