SD卡工具类(SDCardUtils)

本文介绍了一个实用的SD卡操作工具类,包括判断SD卡是否挂载、获取SD卡根目录、总容量及剩余容量的方法。此外,还提供了向SD卡的公共目录和私有目录保存数据的功能,并介绍了如何从SD卡读取数据。
/**
 * 判断SD卡是否挂载
 * 获取SD卡的根目录
 * 获取SD卡总容量
 * 获取SD卡剩余容量
 * 向SD卡9大共有目录保存数据
 * 向SD卡私有File目录写入数据
 * 向SD卡私有Cache目录写入数据
 * 从SD卡中读取数据
 *
 */
public class SDCardUtils {

    /**
     * 判断SD卡是否挂载
     */
    public static boolean isSDCardMounted(){
        String state = Environment.getExternalStorageState();
        return state.equals(Environment.MEDIA_MOUNTED);
    }
    
    /**
     * 返回SD卡根目录
     * @return
     */
    public static String getSDCardRootDir() {
        return Environment.getExternalStorageDirectory().getPath();
    }
    
    /**
     * 获取SD卡总容量
     */
    public static long getSDCardTotalSize(){
        if(isSDCardMounted()){
            //StatFs Statistic File System  簇
            StatFs sf = new StatFs(getSDCardRootDir());
            int blockSize = sf.getBlockSize();
            int blockCount = sf.getBlockCount();
            
            return blockSize * blockCount / 1024 / 1024;  //返回MB
        }
        return 0;
    }
    
    /**
     * 获取SD卡可用容量
     * @return
     */
    public static long getSDCardAvailableSize(){
        if(isSDCardMounted()){
            StatFs sf = new StatFs(getSDCardRootDir());
            int availableBlockCount = sf.getAvailableBlocks();
            int blockSize = sf.getBlockSize();
            
            return availableBlockCount * blockSize / 1024 / 1024;
        }
        return 0;
    }
    
    /**
     * 向SD卡9大共有目录保存数据
     * @param data 需要保存的数据
     * @param type 区分9大共有目录的type
     * @param fileName 另存为的文件名称
     * @return
     */
    public static boolean saveFileToPublicDirectory(byte[] data,
            String type, String fileName){
        if(isSDCardMounted()){
            BufferedOutputStream bos = null;
            File fileDir =
                    Environment.getExternalStoragePublicDirectory(type);
            File file = new File(fileDir, fileName);
            if(!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                bos = new BufferedOutputStream(new FileOutputStream(file));
                bos.write(data);
                bos.flush();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return false;
    }
    
    /**
     * 向SD卡私有File目录写入数据
     * @param context 上下文,用来找到storage/sdcard0/Android/data/packageName/files
     * @param data 需要保存的数据
     * @param type 文件加类型
     * @param fileName 另存为的文件名名称
     * @return
     */
    public static boolean saveFileToExternalFileDir(Context context, byte[] data,
            String type, String fileName){
        if(isSDCardMounted()){
            BufferedOutputStream bos = null;
            File fileDir = context.getExternalFilesDir(null);
            File file = new File(fileDir, fileName);
            if(!file.exists()) {
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                bos = new BufferedOutputStream(new FileOutputStream(file));
                bos.write(data);
                bos.flush();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return false;
    }
    
    /**
     * 向SD卡私有Cache目录写入数据
     * @param context 上下文,用来找到storage/sdcard0/Android/data/packageName/cache
     * @param data 需要保存的数据
     * @param type 文件加类型
     * @param fileName 另存为的文件名名称
     * @return
     */
    public static boolean saveFileToExternalCacheDir(Context context, byte[] data,
            String fileName){
        if(isSDCardMounted()){
            BufferedOutputStream bos = null;
            File fileDir = context.getExternalCacheDir();
            File file = new File(fileDir, fileName);
            if(!file.exists()){
                try {
                    file.createNewFile();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            try {
                bos = new BufferedOutputStream(new FileOutputStream(file));
                bos.write(data);
                bos.flush();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if(bos != null) {
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return false;
    }
    
    /**
     * 从SD卡中读取数据
     * @param fileAbsolutePath  读取文件的绝对路径
     * @return
     */
    public static byte[] loadDataFromSDCard(String fileAbsolutePath){
        if(isSDCardMounted()) {
            BufferedInputStream bis = null;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            
            try {
                bis = new BufferedInputStream(
                        new FileInputStream(fileAbsolutePath));
                byte[] buffer = new byte[1024 * 8];
                int len = 0;
                while((len = bis.read(buffer)) != -1){
                    baos.write(buffer, 0, len);
                    baos.flush();
                }
                return baos.toByteArray();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (bis != null) {
                        bis.close();
                    }
                    if(baos != null) {
                        baos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }
    
    public static String getSDCardCacheDir(Context context){
        return context.getExternalCacheDir().getPath();
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值