美文网首页
照片压缩

照片压缩

作者: d655059c22cc | 来源:发表于2017-09-15 15:15 被阅读0次
     //压缩图片
        public Bitmap compressBitmap(int i) throws IOException {
            // 尺寸压缩倍数,值越大,图片尺寸越小
            int ratio = 2;
            // 压缩Bitmap到对应尺寸
            Bitmap bmp =BitmapFactory.decodeByteArray(items.get(i).getImageAsBytes(), 0, items.get(i).getImageAsBytes().length);
            Bitmap result = Bitmap.createBitmap(bmp.getWidth() / ratio, bmp.getHeight() / ratio, Bitmap.Config.ARGB_8888);
            Canvas canvas = new Canvas(result);
            Rect rect = new Rect(0, 0, bmp.getWidth() / ratio, bmp.getHeight() / ratio);
            canvas.drawBitmap(bmp, null, rect, null);
            return result;
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            String path = Environment.getExternalStorageDirectory()+ "/Image/";
            File dirFile  = new File(path);  //目录转化成文件夹
            if (!dirFile .exists()) {       //如果不存在,建立这个文件夹
                dirFile .mkdirs();
            }
            //保存图片到文件夹
            File file = new File(path, items.get(i).getMessage()+ ".jpg");
            try {
                FileOutputStream  out = new FileOutputStream(file);
                // 把压缩后的数据存放到out中
                result.compress(Bitmap.CompressFormat.JPEG, 100 ,out);
                out.flush();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    

    相关文章

      网友评论

          本文标题:照片压缩

          本文链接:https://www.haomeiwen.com/subject/ihnssxtx.html