gridview图片过大,连续几次打开关闭程序就会溢出内存该怎么办
来源:互联网 发布:top域名需要备案吗 编辑:程序博客网 时间:2024/05/17 20:10
在onpause中
GridView gridView = (GridView)findViewById(R.id.public_photos);
int count = gridView.getCount();
for(int i=0; i < count; i++) {
ImageView v = (ImageView) gridView.getChildAt(i);
if(v != null) {
if(v.getDrawable() != null)
v.getDrawable().setCallback(null);
}
}
Bitmap的OOM确实很让人头疼,所以在制造缩略图的时候直接把图片全部载入内存确实不合适,BitmapFactory.Options有个熟悉叫inJustDecodeBounds,可以把图片载入内存,但是可以获取一些边界信息,也就是图片大小信息。所以可以先预读取一次,算成缩放值,再按照该大小读取图片,具体的代码
BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; // 获取这个图片的宽和高 Bitmap bitmap = BitmapFactory.decodeFile("/sdcard/fortest.jpg", options); //此时返回bm为空 options.inJustDecodeBounds = false; //计算缩放比 int rate = (int)(options.outHeight / (float)100); if (rate <= 0) rate = 1; options.inSampleSize = rate; //重新读入图片,注意这次要把options.inJustDecodeBounds 设为 false哦 bitmap=BitmapFactory.decodeFile("/sdcard/fortest.jpg",options); int w = bitmap.getWidth(); int h = bitmap.getHeight(); ImageView im=new ImageView(this); im.setImageBitmap(bitmap);
最后,图片存储的时候可以直接用bitmap.compress()方法
try { FileOutputStream out=new FileOutputStream(file); if(bitmap.compress(Bitmap.CompressFormat.PNG, 100, out)){ out.flush(); out.close(); } } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
来自eoe
- gridview图片过大,连续几次打开关闭程序就会溢出内存该怎么办
- Android 图片过大内存溢出解决方案
- BitmapFactory.decodeFile图片过大内存溢出
- Android中图片过大造成内存溢出常用解决方法
- php 数据过大 内存溢出
- 打开组件服务 一点计算机就会自动关闭
- tomcat 内存溢出怎么办
- java PreparedStatement需要关闭,不然会内存溢出
- java PreparedStatement需要关闭,不然会内存溢出
- Android---加载图片 解决图片过大导致的内存溢出问题
- Android中图片过大造成内存溢出,OOM(OutOfMemory)异常解决方法(1)
- Android中图片过大造成内存溢出,OOM(OutOfMemory)异常解决方法(2)
- Android中图片过大造成内存溢出,OOM(OutOfMemory)异常解决方法(3)
- Android中图片过大造成内存溢出,OOM(OutOfMemory)异常解决方法(4)
- 尽量避免因图片(Bitamp)过大而导致内存溢出 工具类
- Android中图片过大造成内存溢出,OOM(OutOfMemory)异常解决方法
- 安卓加载图片过大而导致OOM内存溢出的解决方法(巨坑....)
- 为什么我按CTRL+空格键就会关闭程序
- java多线程--OO方式
- Java的clone机制及其可变类与不可变类
- JQuery事件
- 我为什么要学习Hibernate
- linux内核中的min、max函数
- gridview图片过大,连续几次打开关闭程序就会溢出内存该怎么办
- Java正则表达式入门
- 计算机语言的灵魂:范式
- Struts2 + Hibernate开发笔记(一)
- Halcon学习之五:有关图像的定义域的函数
- 使用SVG图片时报错"*** missing separator. Stop."的原因
- sql server2000权限管理
- Qt 应用技巧
- Android的垃圾回收机制(android refbase类(sp wp))