If you already have the original bitmap in memory, you don't need to do the whole process of inJustDecodeBounds
, inSampleSize
, etc. You just need to figure out what ratio to use and scale accordingly.
final int maxSize = 960;
int outWidth;
int outHeight;
int inWidth = myBitmap.getWidth();
int inHeight = myBitmap.getHeight();
if(inWidth > inHeight){
outWidth = maxSize;
outHeight = (inHeight * maxSize) / inWidth;
} else {
outHeight = maxSize;
outWidth = (inWidth * maxSize) / inHeight;
}
Bitmap resizedBitmap = Bitmap.createScaledBitmap(myBitmap, outWidth, outHeight, false);
If the only use for this image is a scaled version, you're better off using Tobiel's answer, to minimize memory usage.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…