写代码的过程中,遇到了关于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所以会变大。特此记录一下。