How to compress image size?

You can create bitmap with captured image as below:

Bitmap bitmap = Bitmap.createScaledBitmap(capturedImage, width, height, true);

Here you can specify width and height of the bitmap that you want to set to your ImageView. The height and width you can set according to the screen dpi of the device also, by reading the screen dpi of different devices programmatically.


Use Compressor (Image compression library). Visit zetbaitsu/Compressor for code and documentation.

  1. add dependency in gradle (app level)

    dependencies {
        implementation 'id.zelory:compressor:3.0.0'
    }
    
  2. To Compress Image use one of these approaches:

  • Compress Image File:

          val compressedImageFile = Compressor.compress(context, actualImageFile)
    
  • Compress Image File to Bitmap:

          val compressedImageFile = Compressor.compress(context, actualImageFile)
          val bitmap = BitmapFactory.decodeFile(compressedImageFile.path)