android获取sdcard可用、已用空间大小的方法:
/**
* 获取sdcard使用情况
*/
private void setSdcardInfo(){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
File filePath = Environment.getExternalStorageDirectory(); //获得sd卡的路径
StatFs stat=new StatFs(filePath.getPath()); //创建StatFs对象
long blockSize=stat.getBlockSize(); //获取block的size
float totalBlocks=stat.getBlockCount(); //获取block的总数
float totalGbSize = (blockSize*totalBlocks)/1024/1024/1024;
DecimalFormat df = new DecimalFormat("#0.0");
String totalGbSizeStr = df.format(totalGbSize); //总共大小
long availableBlocks=stat.getAvailableBlocks(); //获取可用块大小
String usedTotalGbSizeStr = df.format((totalBlocks – availableBlocks) * blockSize /1024/1024/1024);//已用大小
System.out.println("存储空间"+totalGbSizeStr+"G,已用"+usedTotalGbSizeStr+"G");
}
else{
Toast.makeText(this,"SD卡不存在",Toast.LENGTH_LONG).show();
}
}
原创内容转载请保留出处GEEK笔记(http://www.geekapp.cn/)。