保存图片文件的工具类
来源:互联网 发布:endnote 在线数据库 编辑:程序博客网 时间:2024/06/16 19:49
用法 先定义一个 全局变量
private SaveToSdCardUtil saveToSdCardUtil;
初始化工具类
在oncreate 中
saveToSdCardUtil = new SaveToSdCardUtil(context);
先得到 图片的资源 或者 通过url 下载图片得到Bitmap 图片
一参 为 要保存的图片的名字 二参 为 保存的图片资源 没有 填0 三参 为 要保存的 Bitmap 图片 没有填 null
一参 必须 指定 , 二 , 三 参数 根据需求 自行设置
saveToSdCardUtil.saveImageFile(“xxx.jpg”, 0, bmp);
下面是工具类 直接 粘贴 至 工程中 即可 使用
import android.content.Context;import android.content.Intent;import android.graphics.Bitmap;import android.graphics.BitmapFactory;import android.net.Uri;import android.os.Environment;import android.util.Log;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;/** * Created by 保存图片工具类on 2016/7/29. */public class SaveToSdCardUtil { private Context context; //要存的文件绝对路径 String filePath; //要存的文件绝对路径 private String fileCashPath; public SaveToSdCardUtil(Context contexts) { this.context = contexts; } //保存图片文件 public void saveImageFile(String fileName, int imageRes, Bitmap bitmap) { saveToCash(fileName, imageRes, bitmap); //要存的文件 File saveFile; try { if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState()) && Environment.getExternalStorageDirectory().exists()) { filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + fileName;// /storage/sdcard0 saveFile = new File(filePath); } else { filePath = context.getFilesDir().getAbsolutePath() + File.separator + fileName; saveFile = new File(filePath); String cmd = "chmod 777 " + saveFile.getAbsolutePath(); try { Runtime.getRuntime().exec(cmd); } catch (IOException e) { e.printStackTrace(); } } //如果存在这个文件则先删除 if (saveFile.exists()) { saveFile.delete(); } // TODO 1 saveFile.createNewFile(); Bitmap pic = null; if (bitmap != null) { pic = bitmap; } else { pic = BitmapFactory.decodeResource(context.getResources(), imageRes); } context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(saveFile))); FileOutputStream fos = new FileOutputStream(saveFile); pic.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); // java.io.IOException: open failed: EACCES (Permission denied) } catch (Throwable t) { t.printStackTrace(); } } private void saveToCash(String fileName, int imageRes, Bitmap bitmap) { //要存的文件 File saveCashFile; fileCashPath = context.getCacheDir().getAbsolutePath() + File.separator + fileName; //创建文件 saveCashFile = new File(fileCashPath); //如果存在这个文件则先删除 if (saveCashFile.exists()) { saveCashFile.delete(); } try { saveCashFile.createNewFile(); Bitmap pic = null; if (bitmap != null) { pic = bitmap; } else { pic = BitmapFactory.decodeResource(context.getResources(), imageRes); } FileOutputStream fos = new FileOutputStream(saveCashFile); pic.compress(Bitmap.CompressFormat.JPEG, 100, fos); fos.flush(); fos.close(); } catch (IOException e) { e.printStackTrace(); } } public String getFilePath() { if (filePath != null && !filePath.equals("")) { File testFile = new File(filePath); if (!testFile.exists()) { return fileCashPath; } else { Log.i("tag", "-filePath--" + filePath); return filePath; } } else { return fileCashPath; } } //保存文件 public String getSaveFilePath(String fileName) { //要存的文件 File saveFile = null; try { if (Environment.MEDIA_MOUNTED.equals(Environment .getExternalStorageState()) && Environment.getExternalStorageDirectory().exists()) { filePath = Environment.getExternalStorageDirectory() .getAbsolutePath() + File.separator + fileName; //创建文件 saveFile = new File(filePath); } else { filePath = context.getFilesDir().getAbsolutePath() + File.separator + fileName; //创建文件 saveFile = new File(filePath); String cmd = "chmod 777 " + saveFile.getAbsolutePath(); try { Runtime.getRuntime().exec(cmd); } catch (IOException e) { e.printStackTrace(); } } //如果存在这个文件则先删除 if (saveFile.exists()) { saveFile.delete(); } saveFile.createNewFile(); } catch (Throwable t) { t.printStackTrace(); } if (saveFile.getAbsolutePath().toString() != null && !saveFile.getAbsolutePath().toString().equals("")) { return saveFile.getAbsolutePath().toString(); } else { return "/sdcard/" + fileName; } }}
0 0
- 保存图片文件的工具类
- Android 获取手机图片保存的一个通用工具类
- 将图片保存到相册的工具类
- java保存文件工具类
- 文件保存路径工具类
- SharedPreferences保存文件工具类
- 能保存Properties文件注释的Properties工具类
- SpringMVC保存文件工具类--FileUtil
- Java文件保存与删除工具类
- java文件保存工具类【工具包系列】
- Android文件类保存图片等等
- 保存图片到文件
- PHP保存base64格式的图片内容为图片文件
- 图片文件上传工具类
- 适配所有机型的保存图片工具
- 一个在用的文件(图片)上传工具类
- 图片相关操作的工具类 下载 ,缓存到文件
- javaWeb文件以及图片上传smartupload工具类的使用
- 邮件发送的原理
- Swift使用Main.storyboard的segue值来回传数据
- Redux-Saga在React工程架构之的应用实践详解
- 116. Populating Next Right Pointers in Each Node
- [LeetCode]453. Minimum Moves to Equal Array Elements
- 保存图片文件的工具类
- 15. 3Sum hash解法 vs 双指针解法
- 生命倒计时-倒数9117日
- SQL数据库
- 由夏时令引起的java世界时间不一致问题
- 锁
- 【J2EE】jsp分页问题
- python自然语言处理学习笔记-信息提取结构
- 转自实验楼 UDP传输协议