Android如何檢測外置卡的狀態及內置SDcard狀態

外置卡狀態檢測操作,實際上是反射StorageManager這個類,調用StorageManager類裏面的getVolumeState私有方法得到,代碼實現方式如下:

package com.asir.mediaplayer;

import java.util.ArrayList;

import android.content.Context;
import android.os.Environment;
import android.os.storage.StorageManager;

import android.util.Log;

import java.io.File;
import java.io.FileReader;
import java.io.BufferedReader;

/**
 * @brief This class provides a number of function that can get storage device
 *        paths/state or mount/umount storage devices to the upper application.
 **/
public class EnvironmentManager {
    private static final String LOG_TAG = "Asir";
    private static final String SD_PATH_MARK = "ext_sd";
    private static final String USB_PATH_MARK = "udisk";
    private StorageManager sm;

    /**
     * Constructor
     * 
     * @param ctx
     *            Activity Context
     *
     **/
    public EnvironmentManager(Context ctx) {

        sm = (StorageManager) ctx.getSystemService(Context.STORAGE_SERVICE);
    }

    /**
     * @brief get list of paths of all storage
     *
     * @return return storage paths stored in a string array. eg: "mnt/udisk1",
     *         "mnt/ext_sdcard1" ...
     **/
    public String[] getStorageAllPaths() {

        String paths[] = null;

        try {
            paths = (String[]) sm.getClass().getMethod("getVolumePaths").invoke(sm);
        } catch (Exception e) {
            Log.e(LOG_TAG, "Call getMethod of getVolumePaths Error");
            e.printStackTrace();
        }

        return paths;

    }

    /**
     * @brief get paths of all SD storage
     *
     * @return return sd storage paths stored in a string array. eg :
     *         "mnt/ext_sdcard1" ...
     **/
    public String[] getSdAllPaths() {

        ArrayList<String> arrayPath = new ArrayList<String>();
        String paths[] = null;
        int i, count;

        try {
            paths = (String[]) sm.getClass().getMethod("getVolumePaths").invoke(sm);
        } catch (Exception e) {
            Log.e(LOG_TAG, "Call getMethod of getVolumePaths Error");
            e.printStackTrace();
        }

        if (null == paths)
            return null;

        for (i = 0; i < paths.length; i++) {
            if (-1 != paths[i].indexOf(SD_PATH_MARK)) {
                arrayPath.add(paths[i]);
            }
        }

        count = arrayPath.size();

使用方式如下:

package com.asir.album;

import android.content.Context;

public class FileStorageState {

    private EnvironmentManager mEM;

    public FileStorageState(Context context) {
        mEM = new EnvironmentManager(context);
    }

    public boolean getSDState() {
        return queryMeidaState(AppGlobalData.PATH_SD);
    }

    public boolean getUSB1State() {
        return queryMeidaState(AppGlobalData.PATH_USB1);
    }

    public boolean getUSB2State() {
        return queryMeidaState(AppGlobalData.PATH_USB2);
    }

    public boolean getUSB3State() {
        return queryMeidaState(AppGlobalData.PATH_USB3);
    }

    public boolean getUSB4State() {
        return queryMeidaState(AppGlobalData.PATH_USB4);
    }

    public boolean getUSB5State() {
        return queryMeidaState(AppGlobalData.PATH_USB5);
    }

    public boolean queryMeidaState(String path) {
        if (mEM != null) {
            if (mEM.getStorageState(path).equals("mounted")) {
                return true;
            }
        }
        return false;
    }

}

SDcard相關操作,代碼如下:

// 判斷SD卡是否存在
    public static boolean isExistSDCard() {
        if (android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED)) {
            return true;
        }
        return false;
    }

    // 判斷文件是否存在
    public static boolean isUpgradeFileExist(String zipFilePath) {
        boolean result = false;
        if (isExistSDCard()) {
            //"/mnt/sdcard/hcn_mcu_update.zip"
            File file = new File(zipFilePath);
            if (file != null) {
                if (file.exists()) {
                    result = true;
                }
            }
        }
        return result;
    }

    //刪除文件
    private static void delete(File file) {
        if (file.isFile()) {
            file.delete();
            Log.e("file", "delete" + file.getAbsolutePath());
            return;
        }

        if (file.isDirectory()) {
            File[] childFiles = file.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                file.delete();
                return;
            }

            for (int i = 0; i < childFiles.length; i++) {
                delete(childFiles[i]);
            }
            file.delete();
        }

    }

    /**
     * SD卡剩餘空間
     */
    public static long getSDFreeSize() {
        // 取得SD卡文件路徑
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());
        // 獲取單個數據塊的大小(Byte)
        long blockSize = sf.getBlockSize();
        // 空閒的數據塊的數量
        long freeBlocks = sf.getAvailableBlocks();
        // 返回SD卡空閒大小
        // return freeBlocks * blockSize; //單位Byte
        // return (freeBlocks * blockSize)/1024; //單位KB
        return (freeBlocks * blockSize) / 1024 / 1024; // 單位MB
    }

    /**
     * SD卡總容量
     */
    public static long getSDAllSize() {
        // 取得SD卡文件路徑
        File path = Environment.getExternalStorageDirectory();
        StatFs sf = new StatFs(path.getPath());
        // 獲取單個數據塊的大小(Byte)
        long blockSize = sf.getBlockSize();
        // 獲取所有數據塊數
        long allBlocks = sf.getBlockCount();
        // 返回SD卡大小
        // return allBlocks * blockSize; //單位Byte
        // return (allBlocks * blockSize)/1024; //單位KB
        return (allBlocks * blockSize) / 1024 / 1024; // 單位MB
    }

    /*
     * 獲取指定文件大小
     */

    public static long getFileSize(File file) throws Exception {
        long size = 0;
        if (file.exists()) {
            FileInputStream fis = null;
            fis = new FileInputStream(file);
            size = fis.available();
        } else {
            file.createNewFile();
            Log.e("獲取文件大小", "文件不存在!");
        }
        return size / 1024 / 1024;
    }
發佈了70 篇原創文章 · 獲贊 68 · 訪問量 21萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章