首页 > 代码库 > Android下获取手机和SD卡的总空间和可用空间

Android下获取手机和SD卡的总空间和可用空间

获取SD卡的总空间和可用空间

File path = Environment.getExternalStorageDirectory();
StatFs stat = new StatFs(path.getPath());
long blockSize = stat.getBlockSize();
long totalBlocks = stat.getBlockCount();
long availableBlocks = stat.getAvailableBlocks();

long totalSize = totalBlocks * blockSize;
long availSize = availableBlocks * blockSize;

String totalStr = Formatter.formatFileSize(this, totalSize);
String availStr = Formatter.formatFileSize(this, availSize);



获取手机的总空间和可用空间

File path2 = Environment.getDataDirectory();
StatFs stat2 = new StatFs(path2.getPath());
long blockSize2 = stat2.getBlockSize();
long totalBlocks2 = stat2.getBlockCount();
long availableBlocks2 = stat2.getAvailableBlocks();

long totalSize2 = totalBlocks2 * blockSize2;
long availSize2 = availableBlocks2 * blockSize2;

String totalStr2 = Formatter.formatFileSize(this, totalSize2);
String availStr2 = Formatter.formatFileSize(this, availSize2);