提供几个处理图片内存溢出的方法
来源:互联网 发布:ipad淘宝历史版本 编辑:程序博客网 时间:2024/05/18 06:28
/** * 图片按比例大小压缩方法(根据Bitmap图片压缩): * @param image * @return Bitmap */ public Bitmap compressBmpByBm(Bitmap bitmap) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); if (baos.toByteArray().length / 1024 > 1024) {// 判断如果图片大于1M,进行压缩避免在生成图片(BitmapFactory.decodeStream)时溢出 baos.reset();// 重置baos即清空baos bitmap.compress(Bitmap.CompressFormat.JPEG, 50, baos);// 这里压缩50%,把压缩后的数据存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray()); BitmapFactory.Options opts = new BitmapFactory.Options(); // 开始读入图片,此时把options.inJustDecodeBounds 设回true了 opts.inJustDecodeBounds = true; BitmapFactory.decodeStream(isBm, null, opts); opts.inJustDecodeBounds = false; opts.inSampleSize = getSampleSize(opts);// 设置缩放比例 // 重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 isBm = new ByteArrayInputStream(baos.toByteArray()); bitmap = BitmapFactory.decodeStream(isBm, null, opts); return compressImage(bitmap);// 压缩好比例大小后再进行质量压缩 }
// 获取缩放比例值 private int getSampleSize(BitmapFactory.Options opts) {// 获取手机像素 float pW = getMetrics().widthPixels; float pH = getMetrics().heightPixels; int be = 1; if (opts.outWidth > opts.outHeight && opts.outWidth > pW) { be = (int) (opts.outWidth / pW); } else if (opts.outWidth < opts.outHeight && opts.outHeight > pH) { be = (int) (opts.outHeight / pH); } if (be <= 0) { be = 1; } return be; }
public Bitmap compressBmpByResid(int resid) { Bitmap bitmap = null; try { BitmapFactory.Options opts = new BitmapFactory.Options(); // Uri uri = // Uri.parse("android.resource://com.test.oom/drawable-hdpi/hb4.JPG"); opts.inJustDecodeBounds = true; // 节约内存 opts.inPreferredConfig = Bitmap.Config.ARGB_4444; // 默认是Bitmap.Config.ARGB_8888 /* 下面两个字段需要组合使用 */ opts.inPurgeable = true; opts.inInputShareable = true; BitmapFactory.decodeResource(getResources(), resid, opts); opts.inSampleSize = getSampleSize(opts); opts.inJustDecodeBounds = false; // 根据已经得到的缩放比例得到自己想要的图片缩放图 bitmap = BitmapFactory.decodeResource(getResources(), resid, opts);// bitmap = BitmapFactory.decodeStream(getResources().getAssets().open(fileUrl), null, opts); compressImage(bitmap); } catch (Exception e) { e.printStackTrace(); } return bitmap; }
/** * 图片按比例大小压缩方法(根据路径获取图片并压缩): * 路径只支持SDCard的路径像Asset或res不支持 */ public Bitmap compressBmpByPath(String srcPath) { BitmapFactory.Options opts = new BitmapFactory.Options(); //开始读入图片,此时把options.inJustDecodeBounds 设回true了 /** * 设为true那么将不返回实际的bitmap对象,不给其分配内存空间但是可以得到一些解码边界信息即图片大小等信息。 * 因此我们可以通过设置inJustDecodeBounds为true,获取到outHeight(图片原始高度)和 * outWidth(图片的原始宽度 * ),然后计算一个inSampleSize(缩放值),就可以取图片了,这里要注意的是,inSampleSize * 可能等于0,必须做判断。也就是说先将Options的属性inJustDecodeBounds设为true * ,先获取图片的基本大小信息数据 * (信息没有保存在bitmap里面,而是保存在options里面),通过options.outHeight和 options. * outWidth获取的大小信息以及自己想要到得图片大小计算出来缩放比例inSampleSize */ opts.inJustDecodeBounds = true; opts.inPreferredConfig = Bitmap.Config.ARGB_4444; // 默认是Bitmap.Config.ARGB_8888 Bitmap bitmap = BitmapFactory.decodeFile(srcPath,opts);//此时返回bm为空 opts.inJustDecodeBounds = false; opts.inSampleSize = getSampleSize(opts);//设置缩放比例 //重新读入图片,注意此时已经把options.inJustDecodeBounds 设回false了 bitmap = BitmapFactory.decodeFile(srcPath, opts); return compressImage(bitmap);//压缩好比例大小后再进行质量压缩 }
- 提供几个处理图片内存溢出的方法
- 图片内存溢出处理
- android 下的图片内存溢出处理
- 图片内存溢出时的处理
- 防止加载图片内存溢出的方法
- java中内存溢出的处理方法
- Android图片处理内存溢出
- 图片处理(防止内存溢出)
- 【转】内存溢出处理方法
- maven内存溢出处理方法
- myeclipse内存溢出处理的三种处理方法
- Android加载大量网络图片出现内存溢出的处理
- 彻底解决Java处理图片时,BufferedImage内存溢出的解决方案
- C#绘制图片的几个处理方法
- 解决加载图片内存溢出问题的方法
- java中三种常见内存溢出错误的处理方法
- 关于birt报表db数据源内存溢出的处理方法
- java中三种常见内存溢出错误的处理方法
- 在Mac OS X中配置Apache + PHP + MySQL
- 指针作为函数参数传递
- 正则
- MC新手入门(十一)------动画旋转放缩
- WPF进阶之接口(2):IDisposable,ICollectionView
- 提供几个处理图片内存溢出的方法
- java文件名排序
- php中magic_quotes_gpc的作用
- WPF进阶之接口(3):INotifyPropertyChanged,ICommand
- Activity与Intent
- Dns服务的简单配置
- Windows Phone 下载网络图片
- WPF进阶之接口(4):ICommand实现详解
- Java反射详解