Android 按指定大小读取图片的实例
作者:lqh 时间:2022-06-16 18:26:25
在Android开发中,我们经常遇到Android读取图片大小超过屏幕显示的图(一般只要显示一定规格的预览图即可),在图片特别多或者图片显示很频繁的时候要特别注意这个问题,下面介绍个按指定大小读取图像的方法。
实现原理:首先获取图片文件的图像高和宽,如果小于指定比例,则直接读取;如果超过比例则按指定比例压缩读取。
捕获OutOfMemoryError时注意点:后面返回的是null,不要马上从别的地方再读图片,包括R文件中的,不然依然会抛出这个异常,一般在初始化的时候缓存默认图片,然后显示缓存中的图片。
/** 获取图像的宽高**/
public static int[] getImageWH(String path) {
int[] wh = {-1, -1};
if (path == null) {
return wh;
}
File file = new File(path);
if (file.exists() && !file.isDirectory()) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
InputStream is = new FileInputStream(path);
BitmapFactory.decodeStream(is, null, options);
wh[0] = options.outWidth;
wh[1] = options.outHeight;
}
catch (Exception e) {
Log.w(TAG, "getImageWH Exception.", e);
}
}
return wh;
}
public static Bitmap createBitmapByScale(String path, int scale) {
Bitmap bm = null;
try {
//获取宽高
int[] wh = getImageWH(path);
if (wh[0] == -1 || wh[1] == -1) {
return null;
}
//读取图片
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = Math.max(wh[0]/scale, wh[1]/scale);
InputStream is = new FileInputStream(path);
bm = BitmapFactory.decodeStream(is, null, options);
}
catch (Exception e) {
Log.w(TAG, "createBitmapByScale Exception.", e);
}
catch (OutOfMemoryError e) {
Log.w(TAG, "createBitmapByScale OutOfMemoryError.", e);
//TODO: out of memory deal..
}
return bm;
}
标签:Android,图片,指定大小
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Spring MVC学习教程之视图深入解析
2021-12-16 23:37:55
Spring Security账户与密码验证实现过程
2023-03-04 21:54:37
![](https://img.aspxhome.com/file/2023/8/97948_0s.png)
java实现钉钉机器人消息推送的示例代码
2023-05-18 13:53:25
![](https://img.aspxhome.com/file/2023/5/82005_0s.png)
Java容器ArrayList知识点总结
2023-09-05 17:46:04
Java中局部变量和成员变量的区别详解
2021-12-26 11:53:36
![](https://img.aspxhome.com/file/2023/9/85829_0s.webp)
Java中Stream流去除List重复元素的方法
2023-09-04 04:50:33
![](https://img.aspxhome.com/file/2023/0/94210_0s.jpg)
JavaFX实现UI美观效果代码实例
2021-08-27 21:02:15
![](https://img.aspxhome.com/file/2023/6/62206_0s.jpg)
C#使用round函数四舍五入的方法
2022-01-19 04:33:57
必须了解的高阶JAVA枚举特性!
2021-11-04 11:26:00
从最基本的Java工程搭建SpringMVC+SpringDataJPA+Hibernate
2023-05-31 20:37:20
![](https://img.aspxhome.com/file/2023/7/61007_0s.png)
Java开发必备的三大修饰符
2021-10-19 10:11:01
![](https://img.aspxhome.com/file/2023/0/75780_0s.png)
基于FeignException$InternalServerError的解决方案
2023-04-25 15:50:45
![](https://img.aspxhome.com/file/2023/2/60492_0s.png)
Spring Cache+Redis缓存数据的实现示例
2023-11-26 11:53:20
![](https://img.aspxhome.com/file/2023/0/60060_0s.png)
Java流程控制之选择结构
2021-10-24 08:12:50
![](https://img.aspxhome.com/file/2023/4/67464_0s.jpg)
浅析Java中如何实现线程之间通信
2022-08-24 14:28:36
详解Spring与Mybatis整合方法(基于IDEA中的Maven整合)
2023-11-06 21:35:12
![](https://img.aspxhome.com/file/2023/0/58690_0s.png)
一次排查@CacheEvict注解失效的经历及解决
2023-11-13 12:07:23
![](https://img.aspxhome.com/file/2023/0/59200_0s.png)
C#中如何使用Chart图表问题
2023-04-02 16:35:06
![](https://img.aspxhome.com/file/2023/9/104229_0s.png)
Java日常练习题,每天进步一点点(63)
2021-10-22 12:35:44
Java实现AOP面向切面编程的实例教程
2023-02-20 19:32:38