How to dim the size of a Blob image?

Asked

Viewed 133 times

1

I have an image in Blob in the bank, it is in high resolution, I needed to decrease the resolution of it. Does anyone know how to do this? I thank you already.

  • The fact of being in blob has no relation with the problem. You have to read from the blob, do the necessary operation and save in the blob again. If it were a file it would be the same thing.

  • or to reduce it for what purpose?

  • I’m having the error of Outofmemoryerror because my image is too big. On a screen I present several images of small size, I wanted to decrease them since I won’t need them large on the screen that is giving error?

  • You’ll need them "big" somewhere else?

  • yes, when I click on a small image I have to present her "great".

  • in php I do it with the Gd http://php.net/manual/en/function.imagecopyresized.php

  • I was taking the Blob and "setando" inside a Bitmap, but I was able to solve the problem using Bitmapfactory options and using the "inSampleSize" method, worked as I needed. Thanks for the help.

  • um... but this way the blog is sent "great" to the customer and then done the resize, it does not slow down the site ?

Show 3 more comments

1 answer

4


You can decrease the dimensions of an image using the Bitmapfactory.Options passed to Bitmapfactory.decodeByteArray().

Write an auxiliary method to calculate the value of BitmapFactory.Options.inSampleSize:

public static int calculateInSampleSize(
            BitmapFactory.Options options, int reqWidth, int reqHeight) {

    // Altura e largura da imagem original
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        final int halfHeight = height / 2;
        final int halfWidth = width / 2;

         //Calcula o maior valor "inSampleSize" que é uma potência de 2 e mantém tanto
         // a altura e largura maior do que a altura e largura solicitada
        while ((halfHeight / inSampleSize) > reqHeight
                && (halfWidth / inSampleSize) > reqWidth) {
            inSampleSize *= 2;
        }
    }

    return inSampleSize;
}

Write a method that returns a Bitmap with the desired size:

public static Bitmap decodeSampledBitmapFromByteArray(byte[] byteArray,
                                                      int reqWidth, int reqHeight) {

    //Primeiro descodifica com inJustDecodeBounds = true para obter as dimensões originais
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeByteArray(byteArray, 0, byteArray.lenght, options);

    // Calcula o valor inSampleSize a usar para obter as dimensões pretendidas.
    options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);

    // descodifica a imagem com as dimensões pretendidas.
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(byteArray, 0, byteArray.lenght, options);
}

Use like this:

Bitmap imagem = decodeSampledBitmapFromByteArray(byteArray, reqWidth, reqHeight);

Adapted from documentation.

Principles I follow when I use BD and images:

  • "Large" images are always stored in a file on the SD card.
  • When I just need the "big" image, I keep the path to your file.
  • When I need to access a reduced image frequently (px. Listview), I save it in the BD.
  • When I need the "big" and the small image, I save the path Image "large" and reduced image in BD.
  • The "large" image is always taken/read asymptotically.

Browser other questions tagged

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