Reduce size of a Bitmap

1

How do I reduce the size of a Bitmap? For example, take a photo of 600kB and reduce it to 50KB.

obs: No java.

    
asked by anonymous 13.10.2015 / 23:46

1 answer

2

To solve OutOfMemory problem, you should do the following:

BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
Bitmap preview_bitmap = BitmapFactory.decodeStream(is, null, options);

This inSampleSize reduces memory usage.

Here's the complete method. First it reads the image size without decoding the content. Then it finds the best value for inSampleSize , and finally the image is decoded.

// Decodifica a imagem e escala para a redução do consumo de memória
private Bitmap decodeFile(File f) {
    try {
        // Decodifica o tamanho da imagem 
        BitmapFactory.Options o = new BitmapFactory.Options();
        o.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(new FileInputStream(f), null, o);

        // O novo tamanho que queremos 
        final int REQUIRED_SIZE=70;

        // Achar o valor correto para a escala
        int scale = 1;
        while(o.outWidth / scale / 2 >= REQUIRED_SIZE && 
              o.outHeight / scale / 2 >= REQUIRED_SIZE) {
            scale *= 2;
        }

        // Decodifica com o inSampleSize
        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = scale;
        return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
    } catch (FileNotFoundException e) {}
    return null;
}
    
14.10.2015 / 04:45