/** * 獲取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(); } }