Muestra de imágenes y gestión de memoria en Android
Escribí un programa que en cualquier momento muestra 8 imágenes seleccionadas por el usuario en la pantalla. Cada imagen se toma de su forma original y se reduce a un tamaño uniforme. Para hacer esto, estoy usando el siguiente código:
Bitmap bitmapOrg = BitmapFactory.decodeFile(File Location Here);
int width = bitmapOrg.getWidth();
int height = bitmapOrg.getHeight();
int newWidth = 100;
int newHeight = 100;
// calculate the scale - in this case = 0.4f
float scaleWidth = ((float) newWidth) / width;
float scaleHeight = ((float) newHeight) / height;
// createa matrix for the manipulation
Matrix matrix = new Matrix();
// resize the bit map
matrix.postScale(scaleWidth, scaleHeight);
// recreate the new Bitmap
Bitmap resizedBitmap = Bitmap.createBitmap(bitmapOrg, 0, 0, width,
height, matrix, true);
// make a Drawable from Bitmap to allow to set the BitMap
// to the ImageView, ImageButton or what ever
BitmapDrawable bmd = new BitmapDrawable(resizedBitmap);
//ImageView imageView = new ImageView(this);
ImageView iv = (ImageView) findViewById(R.id.imageView1);
// set the Drawable on the ImageView
iv.setImageDrawable(bmd);
// center the Image
iv.setScaleType(ScaleType.CENTER);
Incluso aunque mi código funciona no es perfecto. Parece que estoy usando mucha memoria, especialmente llamando a este código posiblemente 8 veces a la vez. ¿En qué parte del código "reciclaría" la memoria y cómo podría hacer que este código funcione mejor?
EDITAR
Así que implementé el código en mi proyecto y estaba funcionando perfectamente y luego intenté agregarlo a otras secciones y simplemente dejó de funcionar todo junto. Mi código se ve así: ¿alguna idea de qué estoy haciendo mal?
BitmapFactory.Options options = new BitmapFactory.Options();
options.outWidth = 50;
options.outHeight = 50; Bitmap bitmap = BitmapFactory.decodeFile(path, options);
ImageView iv = (ImageView) findViewById(R.id.imageView7);
iv.setImageBitmap(bitmap);