大家都知道在Android平台上,显示图片时,容易出现bitmap outofmemory除了,对即使的回收bitmap,还有个方法,就是在使用前提前对图片进行处理

对图片进行缩放同时也减小了bitmap的大小

/** * 缩放,压缩图片 * @param picPath * @param width * @param height * @return */public InputStream revitionPic(String picPath, int width, int height) {BitmapFactory.Options options = new BitmapFactory.Options();options.inJustDecodeBounds = true;BitmapFactory.decodeFile(picPath, options);options.inSampleSize = calcRatios(options, width, height);options.inJustDecodeBounds = false;Bitmap bitmap = BitmapFactory.decodeFile(picPath, options);                //进一步较小图片的大小return compressPic(bitmap);}
计算缩放尺寸缩放比例

/** * 计算缩放比例 * @param options * @param width * @param height * @return */protected int calcRatios(BitmapFactory.Options options, int width, int height) {int origalWidth = options.outWidth;int origalHeight = options.outHeight;int inSampleSize = 1;if(origalHeight > height || origalWidth > width) {int heightRatios = Math.round(((float)origalHeight / (float)height));int widthRatios = Math.round((float)origalWidth /(float)width);inSampleSize = heightRatios > widthRatios ? widthRatios : heightRatios;}return inSampleSize;}

压缩图片

/** * 压缩图片  * @param pic * @return */public InputStream compressPic(Bitmap pic) {ByteArrayOutputStream baos = new ByteArrayOutputStream();pic.compress(CompressFormat.JPEG, 100, baos);int quality = 100;while(quality > 0 && baos.toByteArray().length / 1024 > 100) {//大于100kbaos.reset();quality -= 5;pic.compress(CompressFormat.JPEG, quality, baos);}return new ByteArrayInputStream(baos.toByteArray());}


更多相关文章

  1. android改变CheckBox的样式
  2. Android(安卓)瀑布流
  3. Android(安卓)无线连接WiFi打印机打印图片全部乱码
  4. Android:混合轮播视频和图片
  5. Android(安卓)加载网络图片 以渐显动画展示
  6. Android强大的图片加载框架Fresco简单用法
  7. Android图片路径与Uri相互转化
  8. android在Canvas使用drawBitmap画一幅画
  9. imageView属性之imageView.setScaleType详解

随机推荐

  1. ScreenUnLock-图形解锁控件使用详解
  2. 基于MVC4+EasyUI开发附件上传组件uploadi
  3. 搭建Visual Stduio 2010开发环境的图文详
  4. 实例分析ASP.NET在MVC5中使用MiniProfile
  5. C#如何计算传入的时间距离今天的时间差的
  6. asp.net页脚制作详解
  7. ASP.NET教程--MVC中SignalR的基础讲解
  8. Asp.net MVC中关于Razor问题的解决方法
  9. 分别介绍MVC、MVP和MVVM是什么
  10. C#中序列化的使用总结