How to get the size of an image stored in a byte array in KB

I want to know the size of an image stored 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 11.7KB image:

 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 

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

+6
source share
1 answer

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

baos.size()/1024 // 81 is the size after image compression

The first gives the size of the bitmap that represents the original image resource , but the next two determine the size of the stream or an array of bytes representing the compressed . Thus, the first returns a larger value than the next two.

+9
source

Source: https://habr.com/ru/post/911578/


All Articles