Android开发中压缩图片的处理

在我们实际开发中,进行的图片压缩使用的还是挺频繁的,今天我们就具体的了解一下:

public Bitmap compressImage(Bitmap image) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    // 质量压缩方法,这里100表示不压缩,把压缩后的数据存放到baos中
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    int options = 100;
    // 循环判断如果压缩后图片是否大于100kb,大于继续压缩
   /* while (baos.toByteArray().length / 1024 > 100) {
        // 重置baos
        baos.reset();
        // 这里压缩options%,把压缩后的数据存放到baos中
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);
        // 每次都减少10
        options -= 10;
    }*/
    // 把压缩后的数据baos存放到ByteArrayInputStream中
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    // 把ByteArrayInputStream数据生成图片
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
    return bitmap;
}

将获取到的压缩图片保存到sdcard并返回路径地址:

public String savePic(Bitmap b) {
    SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss",
            Locale.US);
    File file = Environment.getExternalStorageDirectory();
    // 如果文件不存在,则创建一个新文件
    if (!file.isDirectory()) {
        try {
            file.mkdir();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    String fname = file.getAbsolutePath() + "/Pictures/" + sdf.format(new Date()) + "_order.png";
    File outFile = new File(fname);
    FileOutputStream fos = null;
    try {
        fos = new FileOutputStream(outFile);//获取FileOutputStream对象
        if (fos != null) {
            /**
             * 压缩图片
             * 第一个参数:要压缩成的图片格式
             * 第二个参数:压缩率
             * 第三个参数:压缩到指定位置
             */
            boolean compress = b.compress(Bitmap.CompressFormat.PNG, 90, fos);
            if (compress) {
              // EasyToast.getDEFAULT().show("保存成功");
                //通知图库更新
               /* Intent intent = new Intent();
                intent.setAction(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
                intent.setData(Uri.fromFile(outFile));*/
            } else {

            }
            fos.flush();
            fos.close();//最后关闭此文件输出流并释放与此流相关联的任何系统资源。
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return fname;
}

以上就是讲解的主要的内容

相关推荐

  1. Android开发压缩图片处理

    2023-12-15 11:18:03       40 阅读
  2. Android图片压缩几种方式

    2023-12-15 11:18:03       15 阅读
  3. 【脚本】图片-音视频-压缩文件处理

    2023-12-15 11:18:03       33 阅读
  4. Android系统xml解压与压缩

    2023-12-15 11:18:03       11 阅读
  5. 数据处理图像压缩

    2023-12-15 11:18:03       9 阅读

最近更新

  1. TCP协议是安全的吗?

    2023-12-15 11:18:03       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2023-12-15 11:18:03       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2023-12-15 11:18:03       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2023-12-15 11:18:03       20 阅读

热门阅读

  1. Tinyxml基本用法

    2023-12-15 11:18:03       39 阅读
  2. 使用qemu在arm上模拟x86并运行docker

    2023-12-15 11:18:03       57 阅读
  3. 携程英语测评(已offer)

    2023-12-15 11:18:03       27 阅读
  4. TypeScript基础知识

    2023-12-15 11:18:03       31 阅读
  5. 数据结构-栈

    2023-12-15 11:18:03       36 阅读