-1

I need to build an app which take a picture or select an image from the gallery and I need to send it to the server with a size of 50kb.

Until now, I only find a code in which you can resize the width and height of the bitmap, but thanks to it, the bitmap loses quality.

public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
    int width = image.getWidth();
    int height = image.getHeight();

    float bitmapRatio = (float)width / (float) height;
    if (bitmapRatio > 1) {
        width = maxSize;
        height = (int) (width / bitmapRatio);
    } else {
        height = maxSize;
        width = (int) (height * bitmapRatio);
    }
    return Bitmap.createScaledBitmap(image, width, height, true);
}

Thanks in advance!

lightless07
  • 401
  • 6
  • 17
  • did you try this [link](https://stackoverflow.com/questions/28424942/decrease-image-size-without-losing-its-quality-in-android) – Jyoti JK Dec 25 '17 at 07:22

1 Answers1

1
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
    int options = 100;
    while ( baos.toByteArray().length > 1024*50) { 
        baos.reset();
        image.compress(Bitmap.CompressFormat.JPEG, options, baos);
        options -= 10;
    }
    ByteArrayInputStream isBm = new ByteArrayInputStream(baos.toByteArray());
    Bitmap bitmap = BitmapFactory.decodeStream(isBm, null, null);
    return bitmap;
Inso
  • 837
  • 1
  • 7
  • 11