简体   繁体   中英

Android - size of compressed and uncompressed images the same

I am trying to compress an image and output the size of the compressed and original images to Logcat (using Bitmap.getAllocationByteCount() ). However, the compressed and original image sizes are always the same despite a visible change in the compressed image's quality. I am making use of the Picasso SDK to read images from a resource and eventually load a bitmap returned by the callback in the Target class into an ImageView .

Here is my code:

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);
}
}

I tried compressing JPEGs and PNG with a 0 compression factor, however the sizes are always the same. What am I doing wrong ? An explanation or even a solution would be most appreciated. I am trying to reduce the size of the Bitmap before loading it into an ImageView . Thanks in advance.

Edit: I tried using getByteCount() and getRowBytes() * getHeight() in place of getAllocationByteCount() and the results are always the same.

However, the compressed and original image sizes are always the same despite a visible change in the compressed image's quality.

The size in RAM of a Bitmap has nothing do to with whether the image is compressed or the image quality. It depends solely on resolution (width, height) and bit depth. getAllocationByteCount() returns the size in RAM of the Bitmap .

Compression and image quality affect the size on disk of a bitmap image.

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