安卓文件图片存储工具类
来源:互联网 发布:知乎沙发挑选 编辑:程序博客网 时间:2024/05/16 13:48
转载请保留http://blog.csdn.net/jmheart,jmhart整理发布
package com.androidliujielibs.tools;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 android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.os.Environment;/** * 项目:mzxyCar * @author liujie * 日期:2015-4-26下午2:22:06 * 文件图片的读存 */public class FileService {/** * 保存文件 * @param fileName 文件名称 * @param content 文件内容 * @throws IOException */ public void saveToSDCard(String fileName, String content) throws IOException { //考虑不同版本的sdCard目录不同,采用系统提供的API获取SD卡的目录 File file = new File(Environment.getExternalStorageDirectory(),fileName); if(!file.isDirectory()){ file.createNewFile(); } FileOutputStream fileOutputStream = new FileOutputStream(file); fileOutputStream.write(content.getBytes()); fileOutputStream.close(); } /** * 读取文件内容 * @param fileName 文件名称 * @return 文件内容 * @throws IOException */ public String read(String fileName) throws IOException { File file = new File(Environment.getExternalStorageDirectory(),fileName); if(file.exists()){ FileInputStream fileInputStream=new FileInputStream(file); //把每次读取的内容写入到内存中,然后从内存中获取 ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); byte[] buffer = new byte[1024]; int len =0; //只要没读完,不断的读取 while((len=fileInputStream.read(buffer))!=-1){ outputStream.write(buffer, 0, len); } //得到内存中写入的所有数据 byte[] data = outputStream.toByteArray(); fileInputStream.close(); return new String(data); } else return ""; } /** * 保存图片 * @param b 图片资源 * @param strFileName 图片名称 * @throws IOException */ public void savePhoto(Bitmap b,String strFileName){ try { File file = new File(Environment.getExternalStorageDirectory(),strFileName); if(!file.isDirectory()){ file.createNewFile(); } FileOutputStream fos=new FileOutputStream(file); if(fos!=null){ b.compress(Bitmap.CompressFormat.PNG, 80, fos); fos.flush(); fos.close(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 读取图片 * @param strFileName 图片名称 * @return 图片内容 * @throws IOException */ @SuppressWarnings("unused")public Bitmap readPhoto(String strFileName){ String path=Environment.getExternalStorageDirectory()+"/"+strFileName; if(path!=null){ Bitmap bitmap=BitmapFactory.decodeFile(path); return bitmap; } else return null; }}
0 0
- 安卓文件图片存储工具类
- 安卓上传图片工具类
- 安卓文件存储
- 安卓的文件存储
- 安卓常用工具类-SharePreferenceUtils【偏好存储工具类】
- 安卓:SharedPreferences共享存储的工具类
- 安卓中的外部存储ExternalStorage,工具类
- 安卓数据存储1--文件存储
- 安卓开发27:图片处理工具类
- 安卓使用Picasso封装图片加载工具类
- 安卓工具类
- 安卓常用工具类-FileUtils【文件相关的工具类】
- android文件存储工具类
- 安卓——文件存储
- 安卓文件存储的小小总结
- 安卓文件存储使用参考
- [安卓基础]数据存储之文件
- 安卓文件存储的实现
- Android 开源框架 DataDroid
- hdu 5249 KPI (动态中位数,两个set)
- 黑马程序员——Foundation框架——常用结构体以及NSString相关
- Qt的model中一裤衩函数的总结
- Goroutine + Channel 实践
- 安卓文件图片存储工具类
- 整理一下正则表达式教程,浅显易懂些
- 指针的理解
- 使用属性(C# 编程指南)
- 调整virtualbox 虚拟硬盘大小
- Column count doesn't match value count at row 1解决方法
- 安卓网络管理类
- 千万别跳坑:Robot Framwork 失败case截图、日志输出到指定目录
- Leetcode[189]-Rotate Array