SD卡相關
阿新 • • 發佈:2019-01-04
isSDCardEnable : 判斷SD卡是否可用 getSDCardPath : 獲取SD卡路徑 getDataPath : 獲取SD卡Data路徑 getFreeSpace : 計算SD卡的剩餘空間 getSDCardInfo : 獲取SD卡資訊 public class SDCardUtils { private SDCardUtils() { throw new UnsupportedOperationException("u can't instantiate me..."); } /** * 判斷SD卡是否可用 * * @return true : 可用<br>false : 不可用 */ public static boolean isSDCardEnable() { return Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()); } /** * 獲取SD卡路徑 * <p>先用shell,shell失敗再普通方法獲取,一般是/storage/emulated/0/</p> * * @return SD卡路徑 */ public static String getSDCardPath() { if (!isSDCardEnable()) return "sdcard unable!"; String cmd = "cat /proc/mounts"; Runtime run = Runtime.getRuntime(); BufferedReader bufferedReader = null; try { Process p = run.exec(cmd); bufferedReader = new BufferedReader(new InputStreamReader(new BufferedInputStream(p.getInputStream()))); String lineStr; while ((lineStr = bufferedReader.readLine()) != null) { if (lineStr.contains("sdcard") && lineStr.contains(".android_secure")) { String[] strArray = lineStr.split(" "); if (strArray.length >= 5) { return strArray[1].replace("/.android_secure", "") + File.separator; } } if (p.waitFor() != 0 && p.exitValue() == 1) { return " 命令執行失敗"; } } } catch (Exception e) { e.printStackTrace(); } finally { CloseUtils.closeIO(bufferedReader); } return Environment.getExternalStorageDirectory().getPath() + File.separator; } /** * 獲取SD卡data路徑 * * @return SD卡data路徑 */ public static String getDataPath() { if (!isSDCardEnable()) return "sdcard unable!"; return Environment.getExternalStorageDirectory().getPath() + File.separator + "data" + File.separator; } /** * 獲取SD卡剩餘空間 * * @return SD卡剩餘空間 */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public static String getFreeSpace() { if (!isSDCardEnable()) return "sdcard unable!"; StatFs stat = new StatFs(getSDCardPath()); long blockSize, availableBlocks; availableBlocks = stat.getAvailableBlocksLong(); blockSize = stat.getBlockSizeLong(); return ConvertUtils.byte2FitSize(availableBlocks * blockSize); } /** * 獲取SD卡資訊 * * @return SDCardInfo */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2) public static String getSDCardInfo() { SDCardInfo sd = new SDCardInfo(); if (!isSDCardEnable()) return "sdcard unable!"; sd.isExist = true; StatFs sf = new StatFs(Environment.getExternalStorageDirectory().getPath()); sd.totalBlocks = sf.getBlockCountLong(); sd.blockByteSize = sf.getBlockSizeLong(); sd.availableBlocks = sf.getAvailableBlocksLong(); sd.availableBytes = sf.getAvailableBytes(); sd.freeBlocks = sf.getFreeBlocksLong(); sd.freeBytes = sf.getFreeBytes(); sd.totalBytes = sf.getTotalBytes(); return sd.toString(); } public static class SDCardInfo { boolean isExist; long totalBlocks; long freeBlocks; long availableBlocks; long blockByteSize; long totalBytes; long freeBytes; long availableBytes; @Override public String toString() { return "isExist=" + isExist + "\ntotalBlocks=" + totalBlocks + "\nfreeBlocks=" + freeBlocks + "\navailableBlocks=" + availableBlocks + "\nblockByteSize=" + blockByteSize + "\ntotalBytes=" + totalBytes + "\nfreeBytes=" + freeBytes + "\navailableBytes=" + availableBytes; } } }