android 从文件中读取图片,图片会变大

在进行Bitmap优化时,注意到从文件加载图片后,其大小出现增加。经测试,333333333像素的图片在加载后变为240像素。尽管相关资料较少,一些观点认为从SD卡读取时会生成全质量的BMP,因而造成体积增大。此现象记录以供参考。

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

写代码的过程中,遇到了关于bitmap优化的问题,然后发现了一个现象,即将图片从文件中加载出来,图片大小会变大。

为此我专门测试了一下:

Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bitmap);
try {
    fileOutputStream = new FileOutputStream(file1);
    byteArrayOutputStream = new ByteArrayOutputStream();
    int point = 100;
    bitmap.compress(Bitmap.CompressFormat.JPEG, point, byteArrayOutputStream);
    while (byteArrayOutputStream.toByteArray().length / 1024 > 50) {
        byteArrayOutputStream.reset();
        point -= 10;
        bitmap.compress(Bitmap.CompressFormat.JPEG, point, byteArrayOutputStream);
        Log.e("111111111111", point + "-----------------" + byteArrayOutputStream.toByteArray().length / 1024);
    }

    byteArrayOutputStream.writeTo(fileOutputStream);
    Log.e("22222222222", file1.length() / 1024+"--------------");
} catch (FileNotFoundException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
} 
 
Bitmap bitmap = BitmapFactory.decodeFile(file1.getPath());
File file2 = new File(file, "tupian1.jpg");
try {
    FileOutputStream fileOutputStream1=new FileOutputStream(file2);
    bitmap.compress(Bitmap.CompressFormat.JPEG,100,fileOutputStream1);
    Log.e("333333333", file2.length() / 1024+"--------------");
} catch (FileNotFoundException e) {
    e.printStackTrace();
}
打印的结果是:111111111111: 10-----------------40
22222222222: 40--------------

333333333: 240--------------

查了一下这方面的各位前辈的说法,很少有关于这方面的内容,得到的解释是:从sd卡读取图片时候,是要重新生成全质量的BMP所以会变大。特此记录一下。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值