Reduce the size of a Bitmap

Asked

Viewed 845 times

1

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

Obs:In the java.

  • Please post the information about the image, the maximum amount you need, the resolution, ie your post needs more details.

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 is the complete method. First it reads the image size without decoding the content. Then it finds the best value for the 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;
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.