標籤:
import java.io.File;import android.os.Environment;import android.os.StatFs;//SD卡相關的輔助類public class SDCardUtils{private SDCardUtils(){/* cannot be instantiated */throw new UnsupportedOperationException("cannot be instantiated");}/** * 判斷SDCard是否可用 * * @return */public static boolean isSDCardEnable(){return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);}/** * 擷取SD卡路徑 * * @return */public static String getSDCardPath(){return Environment.getExternalStorageDirectory().getAbsolutePath()+ File.separator;}/** * 擷取SD卡的剩餘容量 單位byte * * @return */public static long getSDCardAllSize(){if (isSDCardEnable()){StatFs stat = new StatFs(getSDCardPath());// 擷取閒置資料區塊的數量long availableBlocks = (long) stat.getAvailableBlocks() - 4;// 擷取單個資料區塊的大小(byte)long freeBlocks = stat.getAvailableBlocks();return freeBlocks * availableBlocks;}return 0;}/** * 擷取指定路徑所在空間的剩餘可用容量位元組數,單位byte * * @param filePath * @return 容量位元組 SDCard可用空間,內部儲存可用空間 */public static long getFreeBytes(String filePath){// 如果是sd卡的下的路徑,則擷取sd卡可用容量if (filePath.startsWith(getSDCardPath())){filePath = getSDCardPath();} else{// 如果是內部儲存的路徑,則擷取記憶體儲存的可用容量filePath = Environment.getDataDirectory().getAbsolutePath();}StatFs stat = new StatFs(filePath);long availableBlocks = (long) stat.getAvailableBlocks() - 4;return stat.getBlockSize() * availableBlocks;}/** * 擷取系統儲存路徑 * * @return */public static String getRootDirectoryPath(){return Environment.getRootDirectory().getAbsolutePath();}}
安卓工具類------>SD卡相關的輔助類