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.
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.
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:
Browser other questions tagged android
You are not signed in. Login or sign up in order to post.
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.
– Bacco
or to reduce it for what purpose?
– ramaral
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?
– daniel12345smith
You’ll need them "big" somewhere else?
– ramaral
yes, when I click on a small image I have to present her "great".
– daniel12345smith
in php I do it with the Gd http://php.net/manual/en/function.imagecopyresized.php
– SneepS NinjA
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.
– daniel12345smith
um... but this way the blog is sent "great" to the customer and then done the resize, it does not slow down the site ?
– SneepS NinjA