Home » Android » android – how to optimize image like Facebook and WhatApp does?

android – how to optimize image like Facebook and WhatApp does?

Posted by: admin May 14, 2020 Leave a comment


I want to optimize the Image file size like whatsapp and facebook are doing. I have send 5MB Image on whatsapp, And received image has size of 80KB. Received image looks identical to the original ones but resolution is less than the original one.

I tried almost all the source codes of android image compression available on stackoverflow but that doesn’t work for me. And then i came across this link to optimize the image, which is doing good work but still not getting the result like whatsapp.

How can i achieve maximum image compression without degrading the quality of image, Same like whatsapp did?

Answer with a source code will be very helpful.

Thanks In advance.

How to&Answers:

You need to decode Image (bitmap)

Here is the code.

public Bitmap  decodeFile(String path) {
        // Decode image size
        int orientation;
        try {
            if (path == null) {
                return null ;
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            // Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE = 70;
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 0;
            while (true) {
                if (width_tmp / 2 < REQUIRED_SIZE
                        || height_tmp / 2 < REQUIRED_SIZE)
                width_tmp /= 2;
                height_tmp /= 2;
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            Bitmap bm = BitmapFactory.decodeFile(path, o2);
            Bitmap bitmap = bm;
            ExifInterface exif = new ExifInterface(path);
            orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);

            Log.e("orientation", "" + orientation);
            bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),bm.getHeight(), null, true);
            bitmapfinal = bitmap;
            return bitmap ;
        } catch (Exception e) {
            return null;



Convert the image into Bitmap and use compress() method to compress it to a certain quality.
To maintain the quality of image, reduce it using aspect ratio of its height and width.you can use below code: where data is the byte array of image

Bitmap screen = BitmapFactory.decodeByteArray(data, 0,
            float oHeight = screen.getHeight();
            float oWidth = screen.getWidth();
            float aspectRatio = oWidth / oHeight;
            int newHeight = 0;
            int newWidth = 0;
            newHeight = 450;
            newWidth = (int) (newHeight * aspectRatio);
            screen = Bitmap.createScaledBitmap(screen, newWidth, newHeight,
ByteArrayOutputStream bos = new ByteArrayOutputStream();
            screen.compress(Bitmap.CompressFormat.JPEG, 50, bos);