Here you can find the source of decodeFromBytes(byte[] bs, int reqWidth, int reqHeight)
public static final Bitmap decodeFromBytes(byte[] bs, int reqWidth, int reqHeight)
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileDescriptor; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.Bitmap.CompressFormat; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.graphics.Paint; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.media.ExifInterface; import android.media.ThumbnailUtils; import android.util.Base64; import android.view.View; public class Main{ public static final Bitmap decodeFromBytes(byte[] bs, int reqWidth, int reqHeight) { if (null == bs) { return null; }/*ww w . j a v a 2 s . c o m*/ BitmapFactory.Options decodeOptions = new BitmapFactory.Options(); Bitmap bitmap = null; decodeOptions.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(bs, 0, bs.length, decodeOptions); int actualWidth = decodeOptions.outWidth; int actualHeight = decodeOptions.outHeight; int desiredWidth = getResizedDimension(reqWidth, reqHeight, actualWidth, actualHeight); int desiredHeight = getResizedDimension(reqHeight, reqWidth, actualHeight, actualWidth); decodeOptions.inJustDecodeBounds = false; decodeOptions.inSampleSize = inSampleSize(decodeOptions, desiredWidth, desiredHeight); Bitmap tempBitmap = BitmapFactory.decodeByteArray(bs, 0, bs.length, decodeOptions); if (tempBitmap != null && (tempBitmap.getWidth() > desiredWidth || tempBitmap .getHeight() > desiredHeight)) { bitmap = Bitmap.createScaledBitmap(tempBitmap, desiredWidth, desiredHeight, true); tempBitmap.recycle(); } else { bitmap = tempBitmap; } return bitmap; } static final int getResizedDimension(int maxPrimary, int maxSecondary, int actualPrimary, int actualSecondary) { if (maxPrimary == 0 && maxSecondary == 0) { return actualPrimary; } if (maxPrimary == 0) { double ratio = (double) maxSecondary / (double) actualSecondary; return (int) (actualPrimary * ratio); } if (maxSecondary == 0) { return maxPrimary; } double ratio = (double) actualSecondary / (double) actualPrimary; int resized = maxPrimary; if (resized * ratio > maxSecondary) { resized = (int) (maxSecondary / ratio); } return resized; } public static final int inSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { if (width > height) { inSampleSize = Math.round((float) height / (float) reqHeight); } else { inSampleSize = Math.round((float) width / (float) reqWidth); } final float totalPixels = width * height; final float totalReqPixelsCap = reqWidth * reqHeight * 2; while (totalPixels / (inSampleSize * inSampleSize) > totalReqPixelsCap) { inSampleSize++; } } return inSampleSize; } }