Proporcionalmente redimensionar un mapa de bits

Tengo un mapa de bits … y si la altura del mapa de bits es mayor que maxHeight, o el ancho es mayor que maxWidth, me gustaría redimensionar proporcionalmente la imagen para que encaje en maxWidth X maxHeight. Esto es lo que intento:

BitmapDrawable bmp = new BitmapDrawable(getResources(), PHOTO_PATH); int width = bmp.getIntrinsicWidth(); int height = bmp.getIntrinsicHeight(); float ratio = (float)width/(float)height; float scaleWidth = width; float scaleHeight = height; if((float)mMaxWidth/(float)mMaxHeight > ratio) { scaleWidth = (float)mMaxHeight * ratio; } else { scaleHeight = (float)mMaxWidth / ratio; } Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap out = Bitmap.createBitmap(bmp.getBitmap(), 0, 0, width, height, matrix, true); try { out.compress(Bitmap.CompressFormat.JPEG, 100, new FileOutputStream(PHOTO_PATH)); } catch(FileNotFoundException fnfe) { fnfe.printStackTrace(); } 

Tengo la siguiente excepción:

java.lang.IllegalArgumentException: bitmap size exceeds 32bits

¿Qué estoy haciendo mal aquí?

Su scaleWidth y scaleHeight deben ser factores de escala (por lo que no son números muy grandes), pero su código parece pasar en el ancho real y la altura que está buscando. Así que terminas aumentando masivamente el tamaño de tu mapa de bits.

Creo que hay otros problemas con el código para derivar scaleWidth y scaleHeight así. Para una cosa su código siempre tiene scaleWidth = width o scaleHeight = height , y cambia sólo uno de ellos, por lo que va a estar distorsionando la relación de aspecto de su imagen también. Si sólo desea cambiar el tamaño de la imagen, entonces sólo debe tener un único scaleFactor .

Además, ¿por qué su declaración if comprueba, efectivamente, maxRatio> ratio ? ¿No debería estar revisando el ancho> maxWidth o height> maxHeight ?

Esto se debe a que el valor de scaleWidth o scaleHeight es demasiado grande, scaleWidth o scaleHeight es la media de la tasa de scaleHeight o encogimiento, pero no la width o la height , la tasa demasiado grande para el tamaño de bitmap de bitmap supera los 32 bits

 matrix.postScale(scaleWidth, scaleHeight); 

Así es como lo hice

 public Bitmap decodeAbtoBm(byte[] b){ Bitmap bm; // prepare object to return // clear system and runtime of rubbish System.gc(); Runtime.getRuntime().gc(); //Decode image size only BitmapFactory.Options oo = new BitmapFactory.Options(); // only decodes size, not the whole image // See Android documentation for more info. oo.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(b, 0, b.length ,oo); //The new size we want to scale to final int REQUIRED_SIZE=200; // Important function to resize proportionally. //Find the correct scale value. It should be the power of 2. int scale=1; while(oo.outWidth/scale/2>=REQUIRED_SIZE && oo.outHeight/scale/2>=REQUIRED_SIZE) scale*=2; // Actual scaler //Decode Options: byte array image with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inSampleSize=scale; // set scaler o2.inPurgeable = true; // for effeciency o2.inInputShareable = true; // Do actual decoding, this takes up resources and could crash // your app if you do not do it properly bm = BitmapFactory.decodeByteArray(b, 0, b.length,o2); // Just to be safe, clear system and runtime of rubbish again! System.gc(); Runtime.getRuntime().gc(); return bm; // return Bitmap to the method that called it } 
FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.