android 对已知路径下图片进行压缩
2017-09-05 00:00
369 查看
用户有很多对图片最大内存进行限制的需求,当图片宽高小于已定标准的宽高时,不对图片做处理,当图片宽高大雨已定标准的宽高时,对图片宽高进行赋值,使大图片满足压缩后满足要求,同时保证不对清晰度小的图片进行处理
int maxWidth=624,maxHeight=840;//定义目标图片的最大宽高,若原图高于这个数值,直接赋值为以上的数值 Bitmap bitmap= BitmapFactory.decodeFile(path); int originWidth=bitmap.getWidth(); int originHeight=bitmap.getHeight(); if(originWidth<maxWidth&&originHeight<maxHeight){ return ; } int width=originWidth; int height=originHeight; if(originWidth>maxWidth) { width=maxWidth; double i = originWidth * 1.0 / maxWidth; height = (int) Math.floor(originHeight / i); bitmap = Bitmap.createScaledBitmap(bitmap, width, height, false); } if(height>maxHeight){ height=maxHeight; bitmap=Bitmap.createBitmap(bitmap,0,0,width,height); } File file=new File(path); if(file.exists()){ file.delete(); } try { FileOutputStream out = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.PNG, 90, 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(); }
相关文章推荐
- android 对已知路径下图片进行压缩
- Android之图片压缩和Uri与String类型的路径转换,获取图片,文件大小的工具类
- android 根据图片url获取bitmap或者drawable,然后再进行压缩处理
- [Android开发]-图片压缩(根据屏幕大小进行比例压缩)
- Android实现从SD卡指定路径获取图片进行轮播
- Android部分手机拍照上传返回为空处理,和拍照后图片压缩后保存到指定路径下
- Android微信分享图片大于32k进行压缩
- Android对图片进行压缩
- Android对图片进行压缩
- Android微信分享图片大于32k进行压缩
- Android选取相册中的图片并对其进行压缩
- Android拍照并保存(对图片进行压缩,分解)
- Android拍照并保存(对图片进行压缩,分解)
- Android开发基础 调用相机 系统相册(并对图片进行压缩处理)
- Android拍照按路径保存图片以及图片压缩不失真
- Android拍照得到全尺寸图片并进行压缩/拍照或者图库选择 压缩后 图片 上传
- Android对图片进行压缩
- android 根据uri获取路径及图片压缩、旋转的学习笔记
- (使用android的图片浏览器打开已知路径的图片文件)Open an image using URI in Android's default gallery image viwer
- Android拍照得到全尺寸图片并进行压缩