Android加载大图Bitmap发生OOM(Out Of Memmory Error)解决方案

简介: 本文作者:Zhang Phil原文链接: http://blog.csdn.net/zhangphil/article/details/48130053Android加载大图Bitmap发生OOM(Out ...
本文作者:Zhang Phil
原文链接:
http://blog.csdn.net/zhangphil/article/details/48130053



Android加载大图Bitmap发生OOM(Out Of Memmory Error)解决方案

Android的内存管理机制对Bitmap比较敏感,并且Android对Bitmap的操作是基于native方法,当在应用中加载过大的Bitmap时候,很容易造成Android OOM(Out Of Memmory) Error,进而导致整个APP崩溃,调试还十分困难。
Android OOM Error如图所示:



对于Android OOM解决方案有很多种,最简单、最便于实施的就是在加载过大Bitmap时候进行压缩,主要是通过设置BitmapFactory.Options的inSampleSize值进行压缩,假设inSampleSize=3,那么,就是说我告知BitmapFactory将原始图片资源缩小到原大小的三分之一。
Java代码所示:

public	static	Bitmap getBitmapNonOOM(Context context, int resId){
		
		BitmapFactory.Options options = new BitmapFactory.Options();
		
		//设置此参数后,将不会把图片载入内存不会分配内存,而只读取图片的基础信息如宽、高。
		//options.inJustDecodeBounds = true;
		
		//BitmapFactory.decodeResource(context.getResources(), resId, options);
		//int imageHeight = options.outHeight;
		//int imageWidth = options.outWidth;
	
		//将原图缩小3倍
		options.inSampleSize = 3;
		
		Bitmap bmp = BitmapFactory.decodeResource(context.getResources(), resId,options);
		
		return	bmp;
	}

正如上述代码中注释掉的那部分代码,

//设置此参数后,将不会把图片载入内存不会分配内存,而只读取图片的基础信息如宽、高。
		//options.inJustDecodeBounds = true;
		
		//BitmapFactory.decodeResource(context.getResources(), resId, options);
		//int imageHeight = options.outHeight;
		//int imageWidth = options.outWidth;


如果加载一个Android图片资源目的仅仅只是为了获取该图片资源的宽高等基础信息,那就没必要将这个图片载入内存,要知道,一旦载入内存,就要带来不小的性能开销,因此,可以对图片资源进行“预处理”,设置BitmapFactory.Options的参数:
options.inJustDecodeBounds = true;

告知BitmapFactory,不要将该图片载入内存,仅仅只需预处理然后返回该图片资源的基础信息如宽高等即可。


相关文章
|
27天前
|
Android开发 开发者
Error:Could not find com.android.support:appcompat-v7:27.0.2.
Error:Could not find com.android.support:appcompat-v7:27.0.2.
16 0
|
2月前
|
Android开发
【Bug】Android resource linking failed和error: failed linking references.
【Bug】Android resource linking failed和error: failed linking references.
|
6月前
|
API Android开发 数据安全/隐私保护
解决android webview 加载http url 失败 net::ERR_CLEARTEXT_NOT_PERMITTED 错误
解决android webview 加载http url 失败 net::ERR_CLEARTEXT_NOT_PERMITTED 错误
238 0
|
3天前
|
安全 编译器 API
Android HAL深入探索(5): 调试HAL报错与解决方案
Android HAL深入探索(5): 调试HAL报错与解决方案
6 1
|
3天前
|
Android开发
Android Mediatek NVRAM 加载 MAC 地址并禁用 MAC 地址更新
Android Mediatek NVRAM 加载 MAC 地址并禁用 MAC 地址更新
6 0
|
16天前
|
Android开发 开发者
安卓投屏神器 Scrcpy安 报错ERROR: Could not find any ADB device
使用Scrcpy安卓投屏工具时遇到报错,问题根源是未开启开发者模式。解决步骤:进入设置,点击【关于手机】→连续点击版本号激活开发者模式,然后在【系统设置】→【开发者选项】中开启USB调试。参照此方法后可正常执行。Scrcpy软件下载链接和GitHub页面也已提供。
26 1
|
28天前
解决Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com
解决Error:All flavors must now belong to a named flavor dimension. Learn more at https://d.android.com
22 5
|
1月前
|
JSON Android开发 数据格式
android 使用GSON 序列化对象出现字段被优化问题解决方案
android 使用GSON 序列化对象出现字段被优化问题解决方案
|
1月前
|
Android开发
[Android jni] Bitmap与Mat对象的相互转换
[Android jni] Bitmap与Mat对象的相互转换
51 0
|
3月前
|
存储 缓存 编解码
Android 性能优化: 解释Bitmap的优化策略。
Android 性能优化: 解释Bitmap的优化策略。
40 1