SD工具类
来源:互联网 发布:苏州德威国际学校 知乎 编辑:程序博客网 时间:2024/06/05 16:03
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 null;}// 获取SD卡的完整空间大小,返回MBpublic static long getSDCardSize() {if (isSDCardMounted()) {StatFs fs = new StatFs(getSDCardBaseDir());long count = fs.getBlockCountLong();long size = fs.getBlockSizeLong();return count * size / 1024 / 1024;}return 0;}// 获取SD卡的剩余空间大小public static long getSDCardFreeSize() {if (isSDCardMounted()) {StatFs fs = new StatFs(getSDCardBaseDir());long count = fs.getFreeBlocksLong();long size = fs.getBlockSizeLong();return count * size / 1024 / 1024;}return 0;}// 获取SD卡的可用空间大小public static long getSDCardAvailableSize() {if (isSDCardMounted()) {StatFs fs = new StatFs(getSDCardBaseDir());long count = fs.getAvailableBlocksLong();long size = fs.getBlockSizeLong();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(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 {bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 {bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 {bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.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 {bos.close();} catch (IOException e) {// TODO Auto-generated catch blocke.printStackTrace();}}}return false;}// 保存bitmap图片到SDCard的私有Cache目录public static boolean saveBitmapToSDCardPrivateCacheDir(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 (Exception e) {e.printStackTrace();} finally {if (bos != null) {try {bos.close();} catch (IOException e) {e.printStackTrace();}}}return true;} else {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 {baos.close();bis.close();} catch (IOException e) {e.printStackTrace();}}return null;}// 从SDCard中寻找指定目录下的文件,返回Bitmappublic 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;}// 获取SD卡公有目录的路径public static String getSDCardPublicDir(String type) {return Environment.getExternalStoragePublicDirectory(type).toString();}// 获取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();}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;}}}
阅读全文
0 0
- SD卡工具类
- SD卡工具类
- SD读写工具类
- SD卡工具类
- SD工具类
- sd卡操作工具类
- 【Android】SD卡工具类
- SD卡工具类(SDCardUtils)
- SD卡操作工具类
- SD卡相关工具类
- Android工具类--SD卡信息类
- SD卡存储图片的工具类
- Android访问SD卡的工具类
- Android的SD卡工具类SDCardUtils
- 安卓工具类------>SD卡相关的辅助类
- Android工具类之SD卡辅助类
- android读取sd卡文件的工具类
- [转载]android读取sd卡文件的工具类
- Unity 光照探针Light Probes
- javadoc 错误 编码gbk的不可映射字符
- 百度分享插件,微信,人人网,新浪微博等等
- Java的内存泄露和内存溢出
- 关于提交页面刷新问题
- SD工具类
- 第2章-创建和销毁对象
- HDU-3591-The trouble of Xiaoqiant
- Android学习之GridLayout
- dijkstra(最短路径)
- Java中的HashMap深入详解
- Dreamweaver 怎么网页布局 div css布局
- A
- Linux 集成环境mvn test 单元测试报错java.lang.OutOfMemoryError: PermGen space