安卓工具類------>SD卡相關的輔助類

來源:互聯網
上載者:User

標籤:

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卡相關的輔助類

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.