公共代码参考(SDCard)

package com.fredric.util;



/*-

 * SD卡操作的工具类封装

 */



import java.io.File;

import android.os.Environment;

import android.os.StatFs;



public class SDCardUtils {



    //判断SA卡是否被正常加载

    //Environment是android一个访问环境变量的类

    //MEDIA_MOUNTED SD卡正常挂载

    //MEDIA_REMOVED 无介质

    //MEDIA_UNMOUNTED 有介质,未挂载

    //MEDIA_BAD_REMOVAL SD卡被取出

    //MEDIA_CHECKING 正在磁盘检查

    //MEDIA_SHARED 操作打开USB存储

    //MEDIA_UNMOUNTABLE 存在介质损坏

    public static boolean isSDCardEnable(){        

        return Environment.getExternalStorageState()

                .equals(Environment.MEDIA_MOUNTED);

    }

    

    //获取SD卡路径

    public static String getSDCardPath(){

        return Environment.getExternalStorageDirectory().getAbsolutePath()

                + File.separator; //增加'/'

    }

    

    //获取SA卡当前容量(单位M)

    public static long getSDCardSize(){

        if(isSDCardEnable()){

            

            StatFs stat = new StatFs(getSDCardPath());

            //getBlockCount 为SD卡总block数

            //getAvailableBlocks SD卡上可用的block数

            long num  = (long) stat.getAvailableBlocks();

            long size = (long)stat.getBlockSize();

            return (num * size)/1024/1024;

            

        }else{

            return 0;

        }

    }

    

}

 

你可能感兴趣的:(sdcard)