Android SD卡帮助类
来源:互联网 发布:js 数组方法 indexof 编辑:程序博客网 时间:2024/05/19 14:16
package com.example.retrofitdemo;import android.content.Context;import android.os.Environment;import android.os.StatFs;import android.util.Log;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.ByteArrayOutputStream;import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;public class SDCardHelper { // 判断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(); }}
阅读全文
0 0
- Android SD卡帮助类
- SD卡的帮助类 SDCardHelper
- Android的SD卡文件读写的帮助类——FileHelper,以及查询SD卡是否存在及其容量的方法
- 【Android】SD卡工具类
- Android工具类--SD卡信息类
- Android访问SD卡的工具类
- Android的SD卡工具类SDCardUtils
- Android SD卡存储封装类
- android SD卡
- 配置android sd卡
- Android SD卡操作
- Android 使用SD卡
- android sd 卡操作
- 【android】 使用sd卡
- 遍历Android SD卡
- Android sd卡
- Android sd卡读取
- Android sd卡
- C#基础知识及讲解(借鉴)
- Train Seats Reservation 2017ACM南宁网络赛
- http下载文件
- Python I/O 和 异常
- PhoneGap和Cordova应该用哪一个?
- Android SD卡帮助类
- Scala函数式程序设计 week4 Timely Effects
- 如何通过反射调用对象的方法?
- Spark-2.1.1集群的安装和配置(基于Hadoop-2.7.3)
- iPhone X 苹果官方人机交互指南 手动翻译
- 竟然忘记改标题 what a fuk
- Ubuntu 16.04 LTS + CUDA8.0 + cudnn6.0
- LeetCode TwoSum(两和)
- python3.+安装scrapy框架