简体   繁体   中英

How to get size of an image that is held in a byte array in KB

I want to find out the size of an image that is held in a byte array in KB

Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.mPicture);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object   
byte[] b = baos.toByteArray();

The following logs display two different results for an image that is : 图像的两个不同结果:

Log.d(TAG, "bm size: " + bm.getByteCount()/1024); // 942
Log.d(TAG, "baos size: " + baos.size()/1024); // 81
Log.d(TAG, "byte size: " + b.length/1024); // 81

Which is the correct result or how do I get the correct result?? Any help is appreciated.

bm.getByteCount()/1024 // 942 is the original size of your image

baos.size()/1024 // 81 is the size after the image has been compressed

The first gives the size of bitmap which represents the original image resource but the next two give the size of the stream or the byte array representing the compressed one. So the first one returns a bigger value than the next two.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM