我试图压缩图像并将压缩和原始图像的大小输出到Logcat(使用Bitmap.getAllocationByteCount()
)。但是,尽管压缩图像的质量有明显变化,但压缩后的原始图像大小始终相同。我正在使用Picasso SDK从资源中读取图像,并最终将由Target
类中的回调返回的位图加载到ImageView
中。Android - 压缩和解压缩图像的大小相同
这里是我的代码:
public class MainActivity extends ActionBarActivity {
ImageView imageView1;
private Target target;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView1 = (ImageView)findViewById(R.id.image_view_1);
target = new Target(){
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
Log.v("BITMAP BEFORE",Integer.valueOf(bitmap.getAllocationByteCount()).toString());
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,0,outputStream);
Bitmap decodedMap = BitmapFactory.decodeStream(new ByteArrayInputStream(outputStream.toByteArray()));
imageView1.setImageBitmap(decodedMap);
Log.v("BITMAP AFTER",Integer.valueOf(decodedMap.getAllocationByteCount()).toString());
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
Log.v("ON BITMAP FAILED", "ON BITMAP FAILED");
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
Log.v("ON PREPARE LOAD","ON PREPARE LOAD");
}
};
loadImage();
}
private void loadImage(){
Picasso.with(getApplicationContext()).load(R.drawable.smiley).into(target);
}
}
我试图压缩的JPEG和PNG以0压缩因子,不过尺寸都是一样的。我究竟做错了什么 ?一个解释或甚至解决方案将不胜感激。我试图在加载到ImageView
之前减小位图的大小。提前致谢。
编辑: 我试着用getByteCount()
和getRowBytes() * getHeight()
代替getAllocationByteCount(),结果总是一样的。