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


猜你喜欢
- package com.test; import java.io.FileNotFoundException;&nbs
- 将下列字符串,依照|拆分成数组:String numbers = "1|2|3|4|5";使用split拆分String
- Springboot自带定时任务实现动态配置Cron参数同学们,我今天分享一下SpringBoot动态配置Cron参数。场景是这样子的:后台
- 一、Java语言本身也是多线程,回顾Java创建线程方式如下:1、继承Thread类,(Thread类实现Runnable接口),来个类图加
- 1.首先是屏蔽浏览器右键菜单的问题,用以下代码可以让浏览器用自己的右键菜单:tempBrowser.ContextMenuStrip = t
- 默认情况下,如果应用以 Android Q 为目标平台,则在访问外部存储设备中的文件时会进入过滤视图。应用可以使用 Context.getE
- Redisson分布式锁之前的基于注解的锁有一种锁是基本redis的分布式锁,锁的实现我是基于redisson组件提供的RLock,这篇来看
- 题目:给定一个如下图所示的数字三角形,从顶部出发,在每一结点可以选择移动至其左下方的结点或移动至其右下方的结点,一直走到底层,要求找出一条路
- 四种隔离机制不要忘记:(1,2,4,8)1.read-uncommitted:能够去读那些没有提交的数据(允许脏读的存在)2.read-co
- 曾经遇到过这样的问题,在我的代码中使用了通知栏,一切都正常,但是就是正在进行的通知栏中属于我的程序的那一条总是上下跳来跳去,一闪一闪的。感觉
- @Value获取application.properties配置无效问题无效的原因主要是要注意@Value使用的注意事项:1、不能作用于静态
- 由于要做一个新项目,所以打算做一个简单的图片验证码。先说说思路吧:在服务端,从一个文件夹里面找出8张图片,再把8张图片合并成一张大图,在8个
- 本教程适合新手小白,Java7之前的版本是没有内置JavaFx的,Java7-10是内置JavaFx的,但是到了Java10以后的版本,Or
- 上一篇JavaMail入门第三篇 发送邮件中,我们学会了如何用JavaMail API提供的Transport类发送邮件,同样,JavaMa
- 方法一:使用AnimatedGif库Nuget安装包:Install-Package AnimatedGif -Version 1.0.5h
- Springboot导出文件,前端下载文件后端代码可以把请求设置为post,我这里是Get @RequestMapping(value =
- 1、配置maven环境变量,将maven安装的bin⽬录添加到path路径中(此电脑->属性->高级系统设置->环境变量-
- C#中Directory.GetFiles() 函数的使用C#中Directory.GetFiles(string path , strin
- @GetMapping注解携带参数方式今天突然发现,当我们根据id查询用户信息时,如果不想通过localhost:8080//findOne
- Spring之动态注册bean什么场景下,需要主动向Spring容器注册bean呢?如我之前做个的一个支持扫表的基础平台,使用者只需要添加基