沒有比這更完整的sdcard工具類了

package com.lt.an20_utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.StatFs;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;

/**
 * Created by 風情萬種冷哥哥 on 2016/10/15.
 */
public class SDCardUtils {

    //判斷sd卡是否被掛載
    public static boolean isSDCardMounted(){
        return Environment.getExternalStorageState().equals(
                Environment.MEDIA_MOUNTED);
    }
    /**
     * Created by 風情萬種冷哥哥 on 2016.
     * 獲取sdcard常用目錄
     */
    public static String getSDCardBaseDir(){
        if (isSDCardMounted()){
            return Environment.getExternalStorageDirectory().getAbsolutePath();
        }
        return null;
    }
    //獲取sdcard公有的目錄的路徑
    public static String getSDCardPublicDir(String type){
        if(isSDCardMounted()){
            return Environment.getExternalStoragePublicDirectory(type).toString();
        }
        return null;
    }
    //獲取sdcard私有cache的目錄的路徑
    public static String getSDCardPrivateCacheDir(Context context){
        if (isSDCardMounted()){
            return context.getExternalCacheDir().getAbsolutePath();
        }
        return null;
    }
    //獲取sdcard私有file目錄的路徑
    public static String getSDCardPrivateFilesDir(Context context,String type){
        if (isSDCardMounted()){
            return context.getExternalFilesDir(type).getAbsolutePath();
        }
        return null;
    }
    /**
     * Created by 風情萬種冷哥哥 on 2016.
     * 獲取sdcard空間的大小
     */
    //獲取sdcard的完整空間大小 。返回MB
    public static long getSDCardSize(){
        if (isSDCardMounted()){
            StatFs fs = new StatFs(getSDCardBaseDir());
            int count = fs.getBlockCount();
            int size = fs.getBlockSize();//此處過時了但也沒有更好的方法更新
            return count*size/1024/1024;
        }
        return 0;
    }

    //獲取sdcard的剩餘空間的大小
    public static long getSDCardFreeSize(){
        if (isSDCardMounted()){
            StatFs fs = new StatFs(getSDCardBaseDir());
            int count = fs.getFreeBlocks();
            int size = fs.getBlockSize();
            return count*size/1024/1024;
        }
        return 0;
    }
    //獲取sdcard的可用空間的大小
    public static long getSDCardAvailableSize(){
        if (isSDCardMounted()){
            StatFs fs = new StatFs(getSDCardBaseDir());
            int count = fs.getAvailableBlocks();
            int size = fs.getBlockSize();
            return count*size/1024/1024;

        }
        return 0;
    }
    /**
     * Created by 風情萬種冷哥哥 on 2016.
     * 往sdcard的公有目錄下保存文件
     *
     *
     */
    public static boolean saveFileToSDCardPublicDir(byte[] data,String type,String fileName){
        BufferedOutputStream bos = null;
        if (isSDCardMounted()){
            File file = Environment.getExternalStoragePublicDirectory(type);
            try {
                bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
                bos.write(data);
                bos.flush();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (bos != null){
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

        }
        return false;
    }
    //往sdcard的自定義目錄下保存文件
    public static boolean saveFileToSDCardCustomDir(byte[] data,String dir,String fileName){
        BufferedOutputStream bos = null;
        if (isSDCardMounted()){
            File file = new File(getSDCardBaseDir()+File.separator+dir);
            if (!file.exists()){
                file.mkdirs();//遞歸創建自定義目錄
                try {
                    bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
                    bos.write(data);
                    bos.flush();
                    return true;
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }finally {
                    if (bos != null){
                        try {
                            bos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
            }
            }
        }
        return false;
    }
    //往sdcard的私有files目錄下保存文件
    public static boolean saveFlieToSDCardPrivateFileDir(byte[] data,String type,String fileName,Context context){
        BufferedOutputStream bos = null;
        if (isSDCardMounted()){
            File file = context.getExternalFilesDir(type);
            try {
                bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
                bos.write(data);
                bos.flush();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    if(bos != null){
                        bos.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return false;
    }
    //往sdcard的私有cache目錄下保存文件
    public static boolean saveFileToSDCardPrivateCacheDir(byte[] data,String fileName,Context context){
        BufferedOutputStream bos = null;
        if (isSDCardMounted()){
            File file = context.getExternalCacheDir();
            try {
                bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
                bos.write(data);
                bos.flush();
                return true;
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (bos != null){
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
        return false;
    }
    //保存bitmap圖片到sdcard的私有目錄
    public static boolean saveBitmapToSDCardCacheDir(Bitmap bitmap,String fileName,Context context){
        if (isSDCardMounted()){
            BufferedOutputStream bos = null;
            //獲取私有的cache的緩存目錄
            File file = context.getExternalCacheDir();
            try {
                bos = new BufferedOutputStream(new FileOutputStream(new File(file,fileName)));
                if (fileName != null && (fileName.contains(".png") || fileName.contains(".PNG"))){
                    bitmap.compress(Bitmap.CompressFormat.PNG,100,bos);
                }else {
                    bitmap.compress(Bitmap.CompressFormat.JPEG,100,bos);
                }
                bos.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (bos != null){
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

            }
            return true;
        }else {
            return false;
        }
    }
    //將圖片保存到sdcard公有目錄
    public static boolean saveBitmapToSDCardPublicDir(Bitmap bm,String type,String fileName){
        if (isSDCardMounted()){
            String filepath = getSDCardPublicDir(type)+File.separator+fileName;
            BufferedOutputStream bos = null;
            try {
                bos = new BufferedOutputStream(new FileOutputStream(new File(filepath)));
                bm.compress(Bitmap.CompressFormat.PNG,100,bos);
                bos.flush();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                if (bos != null){
                    try {
                        bos.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                return true;
            }
        }
        return false;
    }
    // 從SD卡獲取文件
    public static byte[] loadFileFromSDCard(String fileDir) {
        BufferedInputStream bis = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();

        try {
            bis = new BufferedInputStream(
                    new FileInputStream(new File(fileDir)));
            byte[] buffer = new byte[8 * 1024];
            int c = 0;
            while ((c = bis.read(buffer)) != -1) {
                baos.write(buffer, 0, c);
                baos.flush();
            }
            return baos.toByteArray();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (baos != null) {
                    baos.close();
                }
                if (bis != null) {
                    bis.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

    // 從SDCard中尋找指定目錄下的文件,返回Bitmap
    public Bitmap loadBitmapFromSDCard(String filePath) {
        byte[] data = loadFileFromSDCard(filePath);
        if (data != null) {
            Bitmap bm = BitmapFactory.decodeByteArray(data, 0, data.length);
            if (bm != null) {
                return bm;
            }
        }
        return null;
    }


    public static boolean isFileExist(String filePath) {
        File file = new File(filePath);
        return file.isFile();
    }

    // 從sdcard中刪除文件
    public static boolean removeFileFromSDCard(String filePath) {
        File file = new File(filePath);
        if (file.exists()) {
            try {
                file.delete();
                return true;
            } catch (Exception e) {
                return false;
            }
        } else {
            return false;
        }
    }

/**
 * Created by 風情萬種冷哥哥 on 2016.
 * 輸入流轉字節數組
 */
    public static byte[] streamToByteArray(InputStream is) {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        int c = 0;
        byte[] buffer = new byte[8 * 1024];
        try {
            while ((c = is.read(buffer)) != -1) {
                baos.write(buffer, 0, c);
                baos.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (baos != null) {
                try {
                    baos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return baos.toByteArray();
    }

   /**
    * Created by 風情萬種冷哥哥 on 2016.
    *      * @return
    */
    public static String streamToString(InputStream is, String charsetName) {
        BufferedInputStream bis = new BufferedInputStream(is);
        StringBuilder sb = new StringBuilder();
        int c = 0;
        byte[] buffer = new byte[8 * 1024];
        try {
            while ((c = bis.read(buffer)) != -1) {
                sb.append(new String(buffer, charsetName));
            }
            return sb.toString();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bis != null) {
                try {
                    bis.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return null;
    }

  /**
   * Created by 風情萬種冷哥哥 on 2016.
   * 字符串轉輸入流
   */
    public static InputStream stringToInputStream(String str) {
        InputStream is = null;
        try {
            is = new ByteArrayInputStream(str.getBytes("UTF-8"));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (is != null) {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return is;
    }
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章