Android 图片处理避免出现oom的方法详解
作者:燊在锦官城_ 发布时间:2023-09-07 07:26:12
标签:android,图片,oom
1. 通过设置采样率压缩
res资源图片压缩 decodeResource
public Bitmap decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
// Calculate inSampleSize
options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = false;
return BitmapFactory.decodeResource(res, resId, options);
}
uri图片压缩 decodeStream
public Bitmap decodeSampledBitmapFromUri(Uri uri, int reqWidth, int reqHeight) {
Bitmap bitmap = null;
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
options.inSampleSize = BitmapUtils.calculateInSampleSize(options,
UtilUnitConversion.dip2px(MyApplication.mContext, reqWidth), UtilUnitConversion.dip2px(MyApplication.mContext, reqHeight));
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri), null, options);
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}
本地File url图片压缩
public static Bitmap getloadlBitmap(String load_url, int width, int height) {
Bitmap bitmap = null;
if (!UtilText.isEmpty(load_url)) {
File file = new File(load_url);
if (file.exists()) {
FileInputStream fs = null;
try {
fs = new FileInputStream(file);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
if (null != fs) {
try {
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inJustDecodeBounds = true;
BitmapFactory.decodeFileDescriptor(fs.getFD(), null, opts);
opts.inDither = false;
opts.inPurgeable = true;
opts.inInputShareable = true;
opts.inTempStorage = new byte[32 * 1024];
opts.inSampleSize = BitmapUtils.calculateInSampleSize(opts,
UtilUnitConversion.dip2px(MyApplication.mContext, width), UtilUnitConversion.dip2px(MyApplication.mContext, height));
opts.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeFileDescriptor(fs.getFD(),
null, opts);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != fs) {
try {
fs.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}
}
return bitmap;
}
根据显示的图片大小进行SampleSize的计算
public int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
if (reqWidth == 0 || reqHeight == 0) {
return 1;
}
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and
// keeps both height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) >= reqHeight && (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
调用方式:
mImageView.setImageBitmap(decodeSampledBitmapFromResource(getResources(), R.id.myImage, 100, 100))
Bitmap bitmap = decodeSampledBitmapFromUri(cropFileUri);
UtilBitmap.setImageBitmap(mContext, mImage,
UtilBitmap.getloadlBitmap(url, 100, 100),
R.drawable.ic_login_head, true);
2. 质量压缩:指定图片缩小到xkb以下
// 压缩到100kb以下
int maxSize = 100 * 1024;
public static Bitmap getBitmapByte(Bitmap oriBitmap, int maxSize) {
ByteArrayOutputStream out = new ByteArrayOutputStream();
oriBitmap.compress(Bitmap.CompressFormat.JPEG, 100, out);
byte[] fileBytes = out.toByteArray();
int be = (maxSize * 100) / fileBytes.length;
if (be > 100) {
be = 100;
}
out.reset();
oriBitmap.compress(Bitmap.CompressFormat.JPEG, be, out);
return oriBitmap;
}
3. 单纯获取图片宽高避免oom的办法
itmapFactory.Options这个类,有一个字段叫做 inJustDecodeBounds 。SDK中对这个成员的说明是这样的:
If set to true, the decoder will return null (no bitmap), but the out...
也就是说,如果我们把它设为true,那么BitmapFactory.decodeFile(String path, Options opt)并不会真的返回一个Bitmap给你,它仅仅会把它的宽,高取回来给你,这样就不会占用太多的内存,也就不会那么频繁的发生OOM了。
/**
* 根据res获取Options,来获取宽高outWidth和options.outHeight
* @param res
* @param resId
* @return
*/
public static BitmapFactory.Options decodeOptionsFromResource(Resources res, int resId) {
// First decode with inJustDecodeBounds=true to check dimensions
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeResource(res, resId, options);
return options;
}
来源:http://www.jianshu.com/p/9b2fe4c9be17
0
投稿
猜你喜欢
- C#时间显示格式一起看下:24小时制this.toolStripStatusLabel1.Text = “您好,欢迎来到XXXX控制系统!”
- 1.项目简绍本项目使用SpringBoot开发,jdbc5.1.48 Mybatis 源码可下载其中涉及功能有:Mybatis的使用,Thy
- java * 类可以分为两种。 静态代理:由程序员创建或特定工具自动生成源代码,再对其编译。在程序运行前,代理类的.class文件就已经存
- synchronized是Java里的一个关键字,起到的一个效果是“监视器锁”~~,它的功能就是保证操作的原子性,同时禁止指令重排序和保证内
- 本文实例分析了C#遍历List并删除某个元素的方法。分享给大家供大家参考。具体如下:1、我们选择用for循环:for(int i=0;i&l
- 项目运行:环境配置:Jdk1.8 + Tomcat8.5 + mysql + Eclispe(IntelliJ IDEA,Eclispe,M
- 一、容器初始化1、源码分析在jdk8的ConcurrentHashMap中一共有5个构造方法,这四个构造方法中都没有对内部的数组
- 对Jpa Entity关系映射中mappedBy的理解mappedBy 单向关系不需要设置该属性,双向关系必须设置,避免双方都建立外键字段数
- 类和结构的区别:1.一个是引用类型一个是值类型Class是引用类型,继承自System.Object类Struct是值类型,继承自Syste
- 在java项目开发中。最开始换行符大家一般是在idea中设置新文件为LF,并且对旧文件通过IDEA下方的点击来更换换行符。很显然,对于几千文
- 平时开发的工作中, 自己组内的很多大佬经常使用Optional的用法, 自己问他们, 这个到底有什么好处呢,他们说可以很好的规避好空指针的问
- 本文实例为大家分享了Java网络编程TCP程序设计的具体代码,供大家参考,具体内容如下[1] TCP编程的主要步骤客户端(client):1
- spring Boot 熟悉后,集成一个外部扩展是一件很容易的事,集成Redis也很简单,看下面步骤配置:一、添加pom依赖
- 手机二维码扫码登录已经成为了现代互联网时代的一种普遍的登录方式。它的出现,极大地方便了用户登录的流程,减少了用户输入用户名和密码的麻烦。在二
- 这里之所以说“浅谈”是因为我这里只是简单的介绍如何使用Visual C#进行图像的读入、保存以及对像素的访问。而不涉及太多的算法。一、读取图
- @AutoConfiguration读取所有jar包下的 /META-INF/spring.factories 并追加到一个 LinkedM
- 实现功能:模拟简单登录功能,登录成功跳转新页面,登录失败在原登录界面提示登录失败信息开发环境:eclipseTomcat-8.0预备知识:H
- 本文实例讲述了java实现mp3合并的方法。分享给大家供大家参考。具体实现方法如下:package test;import java.io.
- 推荐激活教程IntelliJ IDEA 2020最新激活码(亲测有效,可激活至 2089 年)最新idea2021注册码永久激活(激活到21
- Object是类层次结构的根,每个类都可以将Object作为超类。所有类都直接或者间接的继承自该类构造方法:public Object()回