/**
* 獲取硬盤空間
*/
private static long getSdcSpace() {
final String MOUNTS_FILE = "/proc/mounts";
String sdcPath = "";
BufferedReader br = null;
String strLine = null;
String rs = "\\/mnt/sd[a-z]\\/sd[a-z]\\d";
boolean bMounted = false;
try {
br = new BufferedReader(new FileReader(MOUNTS_FILE));
while ((strLine = br.readLine()) != null) {
// Log.d(TAG, "strLine ====>>>"+strLine.toString());
Pattern p = Pattern.compile(rs);
Matcher m = p.matcher(strLine.toString());
while (m.find()) {
bMounted = true;
sdcPath = m.group(0);
Log.d(TAG, "sdc mount path = " + m.group(0));
break;
}
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
br = null;
}app
try {
if (bMounted) {
StatFs stat = new StatFs(sdcPath);
long blockSize = stat.getBlockSize();
long availableBlocks = stat.getAvailableBlocks();
Log.d(TAG, "free sdc space = " + availableBlocks * blockSize);
return availableBlocks * blockSize;
}
} catch (Exception e) {
// TODO: handle exception
}
return 0L;
}spa
//根據字節數生成大小單位get
private static String fileSize(long size) { // 從MB開始統計 long tmpsize = size / (1024 * 1024); StringBuffer sb = new StringBuffer(); if (tmpsize <= 1024) { sb.append(String.valueOf(tmpsize)); sb.append("MB"); } else { if ((tmpsize / 1024) < 1024) { sb.append(String.valueOf(tmpsize / 1024)); sb.append("GB"); sb.append(String.valueOf(tmpsize % 1024)); sb.append("MB"); } else { sb.append(String.valueOf(tmpsize / (1024 * 1024))); sb.append("TB"); tmpsize /= 1024; sb.append(String.valueOf(tmpsize / 1024)); sb.append("GB"); sb.append(String.valueOf(tmpsize % 1024)); sb.append("MB"); } } Log.d(TAG, "sdc space = = " + sb.toString()); return sb.toString(); }