Android 使用Bitmap将自身保存为文件,BitmapFactory从File中解析图片并防止OOM
1、使用Bitmap将自身保存为文件
public boolean saveBitmapAsFile(String name, Bitmap bitmap) {
File saveFile = new File(cacheDirectory, name);
boolean saved = false;
FileOutputStream os = null;
try {
Log.d("FileCache", "Saving File To Cache " + saveFile.getPath());
os = new FileOutputStream(saveFile);
bitmap.compress(CompressFormat.PNG, , os);
os.flush();
os.close();
saved = true;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return saved;
}
2、BitmapFactory从File中解析图片并防止OOM
/** 获得与需要的比例最接近的比例 */
static int calculateInSampleSize(BitmapFactory.Options bitmapOptions, int reqWidth, int reqHeight) {
final int height = bitmapOptions.outHeight;
final int width = bitmapOptions.outWidth;
int sampleSize = ;
if (height > reqHeight || width > reqWidth) {
final int heightRatio = Math.round((float) height / (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
sampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return sampleSize;
} public static Bitmap decodeImage(String filePath) {
/** Decode image size */
BitmapFactory.Options o = new BitmapFactory.Options();
/** 只取宽高防止oom */
o.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, o); int scale=calculateInSampleSize(o, displayStats.maxItemWidthHeight, displayStats.maxItemWidthHeight); BitmapFactory.Options options=new BitmapFactory.Options();
/** Decode with inSampleSize,比直接算出options中的使用更少的内存*/
options.inSampleSize=scale;
/** 内存不足的时候可被擦除 */
options.inPurgeable = true;
/** 深拷贝 */
options.inInputShareable = true; synchronized (DDGControlVar.DECODE_LOCK) {
Bitmap result = BitmapFactory.decodeFile(filePath, options);
return result;
}
}
Android 使用Bitmap将自身保存为文件,BitmapFactory从File中解析图片并防止OOM的更多相关文章
- Android将程序崩溃信息保存本地文件
大家都知道,现在安装Android系统的手机版本和设备千差万别,在模拟器上运行良好的程序安装到某款手机上说不定就出现崩溃的现象,开发者个人不可能购买所有设备逐个调试,所以在程序发布出去之后,如果出现了 ...
- windows保存的文件传输到linux中格式转换
直接从window传输到linux的脚本执行时,会出现以下错误. -bash: xxx: /bin/sh^M: bad interpreter: No such file or directory 解 ...
- net 编译报错:编辑器或项目正在尝试签出在内存中修改的文件,这将导致保存该文件
1,报错提示: 编辑器或项目正在尝试签出在内存中修改的文件,这将导致保存该文件. 在生成过程中保存文件是危险的,这可能会在将来导致不正确的生成输出. 是否仍然继续签出? 2,原因:licenses.l ...
- Android新浪微博客户端(七)——ListView中的图片异步加载、缓存
原文出自:方杰|http://fangjie.info/?p=193转载请注明出处 最终效果演示:http://fangjie.sinaapp.com/?page_id=54 该项目代码已经放到git ...
- 解决Android解析图片的OOM问题!!!(转)
大家好,今天给大家分享的是解决解析图片的出现oom的问题,我们可以用BitmapFactory这里的各种Decode方法,如果图片很小的话,不会出现oom,但是当图片很大的时候 就要用BitmapFa ...
- Android 将Bitmap对象保存为png图片文件
输入:Bitmap对象.保存的路径.保存的文件名 注意路径的最后要带上 '/' 符号 private void saveBitmap(Bitmap bitmap,String path, Strin ...
- 【Android Developers Training】 25. 保存文件
注:本文翻译自Google官方的Android Developers Training文档,译者技术一般,由于喜爱安卓而产生了翻译的念头,纯属个人兴趣爱好. 原文链接:http://developer ...
- Android音频处理——通过AudioRecord去保存PCM文件进行录制,播放,停止,删除功能
Android音频处理--通过AudioRecord去保存PCM文件进行录制,播放,停止,删除功能 音频这方面很博大精深,我这里肯定讲不了什么高级的东西,最多也只是一些基础类知识,首先,我们要介绍一下 ...
- 解决Android Studio 将String类型保存为.txt文件,按下button跳转到文件管理器(解决了保存txt文件到文件管理后,手机打开是乱码的问题)
不知道为什么保存文件后之前打开一直都OK,就突然打开看到变成乱码了,最后解决了 关键:outStream.write(finalContent.getBytes("gbk")); ...
随机推荐
- 检查 统计 异常 通信 time_wait
[root@hadoop1 conf]# netstat -n | grep -v 127.0.0.1 | grep -v :3306 | grep TIME_WAIT | sort -k 5n | ...
- apache httpd 2.4 httpd
This is a wiki containing user-contributed recipes, tips, and tricks for the Apache HTTP Server (aka ...
- Android应用基础学习记录
01_前言 前言,了解了Android的情况.这里也介绍一下本文.本文是记录学习Android应用程序开发过程,视频中使用的Android2.2版本号,我以4.2版本号为基础,找出当中的差异并记录下来 ...
- ajax跨域问题解决(spring boot)
之前用的服务器响应头部添加Access-Control-Allow-Origin: *来解决的 public static void setResp(HttpServletResponse resp) ...
- import-module in $profile
$PROFILE C:\Users\clu\Documents\PowerShell\Microsoft.PowerShell_profile.ps1 Import-Module 'C:\Users\ ...
- YTU 2838: 改错题AB-装置连接
2838: 改错题AB-装置连接 时间限制: 1 Sec 内存限制: 128 MB 提交: 81 解决: 49 题目描述 注:本题只需要提交标记为修改部分之间的代码,请按照C++方式提交. 有AB ...
- YTU 1006: Hero In Maze
1006: Hero In Maze 时间限制: 1000 Sec 内存限制: 64 MB 提交: 72 解决: 22 题目描述 500年前,Jesse是我国最卓越的剑客.他英俊潇洒,而且机智过人 ...
- 腾讯QQ空间应用宽屏接入
QQ 空间接入宽屏. (与腾讯微博分属两个不同平台) 相关文档: 流动应用画布说明 前端页面规范 多区多服场景说明 应用宽屏根据游戏分为两种. 1: 普通游戏,但想要实现宽屏显示. 2: 多区多服 ...
- Excel VBA 入门
一.文件格式 要使用VBA,excel文件必须保存为启用宏的工作簿,即xlsm格式. 二.启动VBA编辑器 打开工作簿后,要启动VBA编辑器,有两种方法,一是在工作表的名字上面点击右键,选择“查看代码 ...
- [软件安装]MYSQL
https://dev.mysql.com/downloads/repo/yum/ wget https://repo.mysql.com//mysql57-community-release-el7 ...