
Excepción de puntero nulo
Tengo un mapa de bits tomado de una cadena Base64 de mi base de datos remota, (encodedImage
es la cadena que representa la imagen con Base64):
profileImage = (ImageView)findViewById(R.id.profileImage);
byte[] imageAsBytes=null;
try {
imageAsBytes = Base64.decode(encodedImage.getBytes());
} catch (IOException e) {e.printStackTrace();}
profileImage.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
);
profileImage es mi ImageView
Ok, pero tengo que cambiar el tamaño de esta imagen antes de mostrarla en mi ImageView
de mi diseño. Tengo que cambiar el tamaño a 120×120.
¿Alguien puede decirme el código para cambiar el tamaño?
Los ejemplos que encontré no se pudieron aplicar a un mapa de bits obtenido de una cadena base64.

usuario432209
Cambiar:
profileImage.setImageBitmap(
BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
Para:
Bitmap b = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length)
profileImage.setImageBitmap(Bitmap.createScaledBitmap(b, 120, 120, false));

jeet.chanchawat
import android.graphics.Matrix
public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) {
int width = bm.getWidth();
int height = bm.getHeight();
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// CREATE A MATRIX FOR THE MANIPULATION
Matrix matrix = new Matrix();
// RESIZE THE BIT MAP
matrix.postScale(scaleWidth, scaleHeight);
// "RECREATE" THE NEW BITMAP
Bitmap resizedBitmap = Bitmap.createBitmap(
bm, 0, 0, width, height, matrix, false);
bm.recycle();
return resizedBitmap;
}
EDITAR: según lo sugerido por @aveschini, he agregado bm.recycle();
para evitar pérdidas de memoria. Tenga en cuenta que, en caso de que esté utilizando el objeto anterior para otros fines, manéjelo en consecuencia.

ZenBalance
Si ya tiene un mapa de bits, puede usar el siguiente código para cambiar el tamaño:
Bitmap originalBitmap = <original initialization>;
Bitmap resizedBitmap = Bitmap.createScaledBitmap(
originalBitmap, newWidth, newHeight, false);

Renato Probst
Escala basada en relación de aspecto:
float aspectRatio = yourSelectedImage.getWidth() /
(float) yourSelectedImage.getHeight();
int width = 480;
int height = Math.round(width / aspectRatio);
yourSelectedImage = Bitmap.createScaledBitmap(
yourSelectedImage, width, height, false);
Para usar la altura como base en lugar del ancho, cambie a:
int height = 480;
int width = Math.round(height * aspectRatio);
Escale un mapa de bits con un tamaño y ancho máximo de destino, manteniendo la relación de aspecto:
int maxHeight = 2000;
int maxWidth = 2000;
float scale = Math.min(((float)maxHeight / bitmap.getWidth()), ((float)maxWidth / bitmap.getHeight()));
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);

Ravi Makvana
prueba este este código:
BitmapDrawable drawable = (BitmapDrawable) imgview.getDrawable();
Bitmap bmp = drawable.getBitmap();
Bitmap b = Bitmap.createScaledBitmap(bmp, 120, 120, false);
Espero que sea útil.

Comunidad
Alguien preguntó cómo mantener la relación de aspecto en esta situación:
Calcule el factor que está usando para escalar y utilícelo para ambas dimensiones. Digamos que quieres que una imagen tenga el 20% de la altura de la pantalla
int scaleToUse = 20; // this will be our percentage
Bitmap bmp = BitmapFactory.decodeResource(
context.getResources(), R.drawable.mypng);
int sizeY = screenResolution.y * scaleToUse / 100;
int sizeX = bmp.getWidth() * sizeY / bmp.getHeight();
Bitmap scaled = Bitmap.createScaledBitmap(bmp, sizeX, sizeY, false);
para obtener la resolución de la pantalla, tiene esta solución: obtenga las dimensiones de la pantalla en píxeles
Posible duplicado de Cambiar tamaño de mapa de bits en Android
– Sagar Pilkwal
10 de mayo de 2016 a las 6:03
@SagarPilkhwal a este se le preguntó primero
– 김선달
1 de agosto de 2020 a las 12:32