Android加载图片导致内存溢出(Out of Memory异常)
来源:互联网 发布:云计算有哪些应用 编辑:程序博客网 时间:2024/05/17 06:59
文章来源:http://zwkufo.blog.163.com/blog/static/2588251201312864034812/
Android在加载大背景图或者大量图片时,经常导致内存溢出(Out of Memory Error),本文根据我处理这些问题的经历及其它开发者的经验,整理解决方案如下(部分代码及文字出处无法考证):
InputStream is = this.getResources().openRawResource(R.drawable.pic1);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inSampleSize = 10; // width,hight设为原来的十分一
Bitmap btp = BitmapFactory.decodeStream(is, null, options);
/**
* 以最省内存的方式读取本地资源的图片
* @param context
* @param resId
* @return
*/
public static Bitmap readBitMap(Context context, int resId){
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inPreferredConfig = Bitmap.Config.RGB_565;
opt.inPurgeable = true;
opt.inInputShareable = true;
// 获取资源图片
InputStream is = context.getResources().openRawResource(resId);
return BitmapFactory.decodeStream(is, null, opt);
}
if(imageView != null && imageView.getDrawable() != null){
Bitmap oldBitmap = ((BitmapDrawable) imageView.getDrawable()).getBitmap();
imageView.setImageDrawable(null);
if(oldBitmap != null){
oldBitmap.recycle();
oldBitmap = null;
}
}
// Other code.
System.gc();
BitmapFactory.Options opts = new BitmapFactory.Options();
// 设置inJustDecodeBounds为true
opts.inJustDecodeBounds = true;
// 使用decodeFile方法得到图片的宽和高
BitmapFactory.decodeFile(path, opts);
// 打印出图片的宽和高
Log.d("example", opts.outWidth + "," + opts.outHeight);
private final static float TARGET_HEAP_UTILIZATION = 0.75f;
// 在程序onCreate时就可以调用
VMRuntime.getRuntime().setTargetHeapUtilization(TARGET_HEAP_UTILIZATION);
private final static int CWJ_HEAP_SIZE = 6 * 1024 * 1024 ;
VMRuntime.getRuntime().setMinimumHeapSize(CWJ_HEAP_SIZE); // 设置最小heap内存为6MB大小。
- Android加载图片导致内存溢出(Out of Memory异常
- Android加载图片导致内存溢出(Out of Memory异常)
- Android加载图片导致内存溢出(Out of Memory异常)
- Android加载图片导致内存溢出(Out of Memory异常)
- Android加载图片导致内存溢出(Out of Memory异常)
- Android加载图片导致内存溢出(Out of Memory异常)
- Android加载图片导致内存溢出(Out of Memory异常)
- Android加载图片导致内存溢出(Out of Memory异常)
- Android加载图片导致内存溢出(Out of Memory异常)
- Android加载图片导致内存溢出(Out of Memory异常)
- Android加载图片导致内存溢出(Out of Memory异常)
- Android加载图片导致内存溢出(Out of Memory异常)
- Android加载图片导致内存溢出(Out of Memory异常)
- Android 加载图片过程导致内存溢出(Out Of Memory)
- Android 图片内存溢出(Out of Memory)
- 安卓加载大背景图片或者加载大量本地资源图片时经常导致内存溢出(Out of Memory Error)推荐几种解决方案
- android内存溢出问题(out of memory)
- Android内存溢出(Out Of Memory)…
- 更新项目引用后发生Newtonsoft.Json引用冲突的解决办法
- Java基础学习-序
- Zookeeper学习笔记
- Adapter简化写法以及findViewById()方法的优化
- HTML高级标签之窗口分帧(前台界面演示)
- Android加载图片导致内存溢出(Out of Memory异常)
- 用3个for和2个for位三位数组赋值
- 图片缓存:浏览器刷新 和 304 Not Modified 与 If-Modified-Since 及 Cache-Control
- linux内核之文件系统
- AndroidMenifest.xml中targetSdkVersion和project.properties中的target属性的区别
- 深入图解字符集与字符集编码(二)——编号 vs 编码(出处:http://my.oschina.net/goldenshaw/blog/305805)
- 不能从const char *转换为LPCWSTR
- Linux笔记(一)-了解Linux
- php中出现Notice: Uninitialized string offset: 0原因及解决办法