/** * 压缩图片 * @param targetwidth 压缩的目标大小 例如将图片等比压缩到 1080 * xxxx 或者 xxxx * 1080 输入 1080 即可 */ private void CompressPic(double targetwidth) { Bitmap bitmap = BitmapFactory.decodeFile(Environment.getExternalStorageDirectory().toString() +"/DCIM/pic.jpg"); //double targetwidth = 1080;//;Math.sqrt(64.00 * 1000);// 1280 Log.i("输出", "图片处理开始.."); // 创建操作图片用的matrix对象 Matrix matrix = new Matrix(); // 计算宽高缩放率 double x = Math.max(targetwidth / bitmap.getWidth(), targetwidth / bitmap.getHeight()); // 缩放图片动作 matrix.postScale((float) x, (float) x); bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); String compressPicPath = Environment.getExternalStorageDirectory().toString() +"/DCIM/pic_cmp.jpg"; File file = new File(compressPicPath); if(file.exists()){ file.delete(); } try { FileOutputStream fos = new FileOutputStream(file); bitmap.compress(Bitmap.CompressFormat.JPEG,100,fos); fos.flush(); fos.close(); } catch (Exception e) { e.printStackTrace(); } }