最好的实现方式应该是app告诉服务端想要的图片尺寸,然后服务端获取图片再将压缩后的图片返回给app,这样既节省了用户流量又降低了app的内存耗费
/** * 压缩图片-质量压缩法 * * @param image 压缩前的bitmap * @param kb 限制压缩后bitmap的大小,单位kb */ public static Bitmap compressImage(Bitmap image, int kb) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); image.compress(Bitmap.CompressFormat.JPEG, 100, baos);//质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中 int options = 100; while (baos.toByteArray().length / 1024 > kb) { //循环判断如果压缩后图片是否大于kb,大于继续压缩 baos.reset();//重置baos即清空baos if (options <= 10) { options -= 3;//每次压缩3% } else { options -= 10;//每次压缩10% } if (options <= 0) break; image.compress(Bitmap.CompressFormat.JPEG, options, baos);//这里压缩options%,把压缩后的数据存放到baos中 } ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());//把压缩后的数据baos存放到ByteArrayInputStream中 return BitmapFactory.decodeStream(isBm, null, null);}
/** * 从图片路径获取图片并压缩-边界压缩法 * * @param srcPath 图片路径 */ public static Bitmap compressImageFromFile(String srcPath) { BitmapFactory.Options newOpts = new BitmapFactory.Options(); newOpts.inJustDecodeBounds = true;//只读边,不读内容 Bitmap bitmap = BitmapFactory.decodeFile(srcPath, newOpts);//只获取边界信息,此时返回bitmap为null int w = newOpts.outWidth; int h = newOpts.outHeight; float hh = 1920f; float ww = 1080f; int be = 1; if (w > h && w > ww) { be = (int) (newOpts.outWidth / ww); } else if (w < h && h > hh) { be = (int) (newOpts.outHeight / hh); } if (be <= 0) be = 1; newOpts.inSampleSize = be;//设置采样率,缩放后图片是原图片大小的几分之几 newOpts.inPreferredConfig = Bitmap.Config.ARGB_8888;//该模式是默认的,可不设 newOpts.inPurgeable = true;// 同时设置才会有效 newOpts.inInputShareable = true;//当系统内存不够时候图片自动被回收 newOpts.inJustDecodeBounds = false; return BitmapFactory.decodeFile(srcPath, newOpts); }