SD卡相關輔助類 SDCardUtils

ed6y 11年前發布 | 2K 次閱讀 Java Android


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();
      }

} </pre>

 本文由用戶 ed6y 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!