Android开发中的内存读写一些工具类
来源:互联网 发布:淘宝网络客服招聘兼职 编辑:程序博客网 时间:2024/05/29 10:08
**在我们实际开发过程中经常会遇到关于手机内存的管理,比如说读取内存卡,下载图片、音乐、视频等等。每次使用的时候比较麻烦,下面这是一个关于手机内存的封装类,大家可以借鉴下:**
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 data, String fileName, Context context) { BufferedInputStream bis = null; BufferedOutputStream bos = null; if (isSDCardMounted()) { File file = context.getExternalCacheDir(); try { bis = new BufferedInputStream(data); 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 (bis != null) { bis.close(); } if (bos != null) { bos.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(); }}
阅读全文
1 0
- Android开发中的内存读写一些工具类
- Android开发中的一些问题小计1(读写数据)
- Android 文件读写工具类
- android 文件读写工具类
- Android开发中的文件的读写
- Android内存工具类
- Android开发中的一些坑
- Android开发中的一些经验总结
- android开发中的一些思考
- Android开发中的一些坑
- Android 开发中的一些建议
- Android 开发中的一些整理
- [Android开发工具类]对控件的一些常用方法
- Android开发自己总结的Adapter的一些工具类
- Android开发中常用到的一些工具类整理
- Android开发一些常用的工具类(实用型)
- Android开发中一些常用的工具类汇总
- Android实际开发中一些比较常用的工具类
- echarts
- 主成分分析(Principal Component Analysis,PCA)详解
- 表单验证(使用AJAX)
- 类型定义
- 深度信念网络
- Android开发中的内存读写一些工具类
- JavaWeb-020-Cookie自动登录
- Git中文件的修改与提交
- Spark SQL读取hive数据时报找不到mysql驱动
- TCP、HTTP 、UDP协议
- 低层视觉:使用多幅图像——多视角几何学
- NodeJs fs 模块
- 【设计模式】代理模式(Proxy Pattern)
- Spring Boot项目打成jar包后关于配置文件的外部化配置