Yanshof Yanshof - 1 year ago 126
Android Question

How to make Bitmap compress without change the bitmap size?

I using this method to compress image

if(bitmapObject.compress(Bitmap.CompressFormat.PNG, 100, fOut))

But that image that i get is much small ( in the dimension ) that was before the compress action.

My application need to send the compressed image thru the network - so i want to send as less data as i can ... but i must to keep the original size of the image.

Is there some other way to keep the original bitmap dimension with some compress ?

Answer Source

Are you sure it is smaller?

Bitmap original = BitmapFactory.decodeStream(getAssets().open("1024x768.jpg"));
ByteArrayOutputStream out = new ByteArrayOutputStream();
original.compress(Bitmap.CompressFormat.PNG, 100, out);
Bitmap decoded = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));

Log.e("Original   dimensions", original.getWidth()+" "+original.getHeight());
Log.e("Compressed dimensions", decoded.getWidth()+" "+decoded.getHeight());


12-07 17:43:36.333: E/Original   dimensions(278): 1024 768
12-07 17:43:36.333: E/Compressed dimensions(278): 1024 768

Maybe you get your bitmap from a resource, in which case the bitmap dimension will depend on the phone screen density

Bitmap bitmap=((BitmapDrawable)getResources().getDrawable(R.drawable.img_1024x768)).getBitmap();
Log.e("Dimensions", bitmap.getWidth()+" "+bitmap.getHeight());

12-07 17:43:38.733: E/Dimensions(278): 768 576
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download