Android图片压缩

该博客介绍了在Android应用中如何实现从图库选择图片并进行尺寸和质量压缩的技术。通过启动意图让用户选择图片,然后使用BitmapFactory的Options进行宽高缩放,最后进行质量压缩,以适应应用需求并减少内存消耗。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

打开图片库

Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, 1);

获取选择的图片

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {
        Uri uri = data.getData();           
        try {
            //处理获取到的uri
        } catch (FileNotFoundException e) {
            showDialogError(e.getMessage());
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}

图片压缩

ContentResolver contentResolver = this.getContentResolver();
//创建 options 实例
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true; //设置为true时,获取到的bitmap为null,只是存放宽高值
Bitmap bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri), null, options);
// 计算宽高缩放比例
int samplew = options.outWidth / 680;
int sampleh = options.outHeight / 680;
// 最终取大的那个为缩放比例,这样才能适配
// 设置缩放比例
options.inSampleSize = Math.max(samplew, sampleh);
options.inJustDecodeBounds = false; // 一定要记得将inJustDecodeBounds设为false,否则Bitmap为null
bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri), null, options);
//以上为尺寸压缩,接着再执行一次质量压缩
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 36, bos);
picData = bos.toByteArray();
Bitmap result = BitmapFactory.decodeByteArray(picData, 0, picData.length);
mView.ivGoodsPic.setImageBitmap(result);

完整代码

//设定控件点击事件打开图库
mView.ivGoodsPic.setOnClickListener(view -> {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, 1);
});
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    if (requestCode == 1 && resultCode == RESULT_OK) {
        Uri uri = data.getData();
        ContentResolver contentResolver = this.getContentResolver();
        try {
            //创建 options 实例
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true; //设置为true时,获取到的bitmap为null,只是存放宽高值
            Bitmap bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri), null, options);
            // 计算宽高缩放比例
            int samplew = options.outWidth / 680;
            int sampleh = options.outHeight / 680;
            // 最终取大的那个为缩放比例,这样才能适配
            // 设置缩放比例
            options.inSampleSize = Math.max(samplew, sampleh);
            options.inJustDecodeBounds = false; // 一定要记得将inJustDecodeBounds设为false,否则Bitmap为null
            bitmap = BitmapFactory.decodeStream(contentResolver.openInputStream(uri), null, options);
            //以上为尺寸压缩,接着再执行一次质量压缩
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 36, bos);
            picData = bos.toByteArray();
            Bitmap result = BitmapFactory.decodeByteArray(picData, 0, picData.length);
            mView.ivGoodsPic.setImageBitmap(result);
        } catch (FileNotFoundException e) {
            showDialogError(e.getMessage());
        }
    }
    super.onActivityResult(requestCode, resultCode, data);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值