SD卡工具類↙↙↙

// 判斷SD卡是否被掛載

public static boolean isSDCardMounted() {

// return Environment.getExternalStorageState().equals("mounted");

? ? return Environment.getExternalStorageState().equals(

Environment.MEDIA_MOUNTED);

}

// 獲取SD卡的跟路徑

public static String getSDCardBaseDir() {

if (isSDCardMounted()) {

return Environment.getExternalStorageDirectory().getAbsolutePath();

}

return "";

}

// 獲取SD卡的完整空間大小,返回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;

}

// 獲取SD卡的剩余空間大小郭宝,返回MB

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;

}

// 獲取SD卡的可用空間大小粘室,返回MB

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;

}

// 往SD卡的公有目錄下保存文件

public static boolean saveFileToSDCardPublicDir(byte[] data, String type,

String fileName) {

BufferedOutputStream bos =null;

if (isSDCardMounted()) {

File file = Environment

.getExternalStoragePublicDirectory(Environment.DIRECTORY_MOVIES);

// Log.i("SDCardHelper", "--->" + file.getAbsolutePath());

? ? ? ? try {

bos =new BufferedOutputStream(new FileOutputStream(new File(

file, fileName)));

bos.write(data);

bos.flush();

return true;

}catch (Exception e) {

e.printStackTrace();

}finally {

try {

if (bos !=null) {

bos.close();

}

}catch (IOException e) {

e.printStackTrace();

}

}

}

return false;

}

// 往SD卡的私有Files目錄下保存文件

public static boolean saveFileToSDCardPrivateFilesDir(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 (Exception e) {

e.printStackTrace();

}finally {

try {

if (bos !=null) {

bos.close();

}

}catch (IOException e) {

e.printStackTrace();

}

}

}

return false;

}

// 往SD卡的私有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 (Exception e) {

e.printStackTrace();

}finally {

try {

if (bos !=null) {

bos.close();

}

}catch (IOException e) {

e.printStackTrace();

}

}

}

return false;

}

// 往SD卡的私有Cache目錄下保存文件

public static boolean saveFileToSDCardPrivateCacheDir(InputStream in,

String fileName, Context context) {

BufferedOutputStream bos =null;

BufferedInputStream bis=null;

if (isSDCardMounted()) {

File file = context.getExternalCacheDir();

String absolutePath = context.getExternalCacheDir().getAbsolutePath();

Log.e("TAG","saveFileToSDCardPrivateCacheDir: "+absolutePath );

try {

bis=new BufferedInputStream(in);

bos =new BufferedOutputStream(new FileOutputStream(new File(

file, fileName)));

byte[] buffer=new byte[1024*8];

int c=0;

while((c=bis.read(buffer))!=-1){

bos.write(buffer,0,c);

bos.flush();

}

return true;

}catch (Exception e) {

e.printStackTrace();

}finally {

try {

if (bos !=null) {

bos.close();

}

if(bis!=null){

bis.close();

}

}catch (IOException e) {

e.printStackTrace();

}

}

}

return false;

}

// 往SD卡的自定義目錄下保存文件

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 (Exception e) {

e.printStackTrace();

}finally {

try {

if (bos !=null) {

bos.close();

}

}catch (IOException e) {

e.printStackTrace();

}

}

}

return false;

}

// 從SD卡獲取文件

public static byte[] loadFileFromSDCard(String filePath) {

BufferedInputStream bis =null;

ByteArrayOutputStream baos =new ByteArrayOutputStream();

try {

bis =new BufferedInputStream(new FileInputStream(

new File(filePath)));

byte[] buffer =new byte[1024 *8];

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 (bis !=null) {

bis.close();

}

if (baos !=null) {

baos.close();

}

}catch (IOException e) {

e.printStackTrace();

}

}

return null;

}

// 獲取SD卡公有目錄的路徑

public static String getSDCardPublicDir(String type) {

return Environment.getExternalStoragePublicDirectory(type)

.getAbsolutePath();

}

// 獲取SD卡私有Cache目錄的路徑

public static String getSDCardPrivateCacheDir(Context context) {

return context.getExternalCacheDir().getAbsolutePath();

}

// 獲取SD卡私有Files目錄的路徑

public static String getSDCardPrivateFilesDir(Context context, String type) {

return context.getExternalFilesDir(type).getAbsolutePath();

}

?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末喳魏,一起剝皮案震驚了整個濱河市刺彩,隨后出現(xiàn)的幾起案子创倔,更是在濱河造成了極大的恐慌,老刑警劉巖霸妹,帶你破解...
    沈念sama閱讀 218,941評論 6 508
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叹螟,死亡現(xiàn)場離奇詭異,居然都是意外死亡畏线,警方通過查閱死者的電腦和手機良价,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,397評論 3 395
  • 文/潘曉璐 我一進店門明垢,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人抵蚊,你說我怎么就攤上這事泌射。” “怎么了熔酷?”我有些...
    開封第一講書人閱讀 165,345評論 0 356
  • 文/不壞的土叔 我叫張陵,是天一觀的道長躺酒。 經常有香客問我,道長羹应,這世上最難降的妖魔是什么园匹? 我笑而不...
    開封第一講書人閱讀 58,851評論 1 295
  • 正文 為了忘掉前任裸违,我火速辦了婚禮供汛,結果婚禮上怔昨,老公的妹妹穿的比我還像新娘雀久。我一直安慰自己岸啡,他們只是感情好,可當我...
    茶點故事閱讀 67,868評論 6 392
  • 文/花漫 我一把揭開白布擂送。 她就那樣靜靜地躺著嘹吨,像睡著了一般蟀拷。 火紅的嫁衣襯著肌膚如雪萍聊。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,688評論 1 305
  • 那天,我揣著相機與錄音挡鞍,去河邊找鬼。 笑死预烙,一個胖子當著我的面吹牛墨微,可吹牛的內容都是我干的。 我是一名探鬼主播扁掸,決...
    沈念sama閱讀 40,414評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼翘县,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了也糊?” 一聲冷哼從身側響起炼蹦,我...
    開封第一講書人閱讀 39,319評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎狸剃,沒想到半個月后掐隐,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經...
    沈念sama閱讀 45,775評論 1 315
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 37,945評論 3 336
  • 正文 我和宋清朗相戀三年虑省,在試婚紗的時候發(fā)現(xiàn)自己被綠了匿刮。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,096評論 1 350
  • 序言:一個原本活蹦亂跳的男人離奇死亡怀大,死狀恐怖潜慎,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤堤结,帶...
    沈念sama閱讀 35,789評論 5 346
  • 正文 年R本政府宣布瘾带,位于F島的核電站抄罕,受9級特大地震影響嚷兔,放射性物質發(fā)生泄漏。R本人自食惡果不足惜耐齐,卻給世界環(huán)境...
    茶點故事閱讀 41,437評論 3 331
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧的畴,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,993評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春棘利,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,107評論 1 271
  • 我被黑心中介騙來泰國打工掌敬, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 48,308評論 3 372
  • 正文 我出身青樓扶供,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子捅儒,可洞房花燭夜當晚...
    茶點故事閱讀 45,037評論 2 355

推薦閱讀更多精彩內容