Cómo cambiar el tamaño de la imagen antes de agregarla al archivo xml

I have many bitmaps in my xml file and I'm getting an out of memory exception.

I have this code to resize the bitmaps to reduce the space.

public static Bitmap decodeSampledBitmapFromResource(Resources res,
            int resId, int reqWidth, int reqHeight) {

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeResource(res, resId, options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeResource(res, resId, options);
    }

How can I use this code to resize the images in my drawable folder BEFORE they are added in the xml file so I dont get an out of memory exception?

preguntado el 27 de noviembre de 13 a las 00:11

2 Respuestas

Puedes hacerlo,

return Bitmap.createScaledBitmap(BitmapFactory.decodeResource(res, resId, options), reqWidth, reqHeight, false);

your image will follow the size of your reqWidth and reqHeight.

respondido 27 nov., 13:03

You can use like this ...

Bitmap decodedBitmap = decodeSampledBitmapFromResource(getResources(), R.drawable.myImage, 100, 100); //100x100 should be imageView display size.
imgView.setImageBitmap(decodedBitmap);

respondido 27 nov., 13:09

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.