Java tutorial
//package com.java2s; //License from project: Apache License import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.BitmapFactory.Options; public class Main { public static Bitmap decodeSampledBitmapFromByteArray(byte[] bytes, long allowedBmpMaxMemorySize) { try { final Options options = new Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options); options.inSampleSize = calculateInSampleSize(options, allowedBmpMaxMemorySize); options.inJustDecodeBounds = false; return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options); } catch (Throwable err) { err.printStackTrace(); } return null; } /** * assume one pix use 4 bytes.Bitmap.Config.ARGB_8888. */ private final static int calculateInSampleSize(Options options, long reqMemorySize) { final int onePixBytes = 4; int reqPixs = (int) (reqMemorySize / onePixBytes); final int height = options.outHeight; final int width = options.outWidth; int orgPixs = height * width; int inSampleSize = 1; while (orgPixs / Math.pow(inSampleSize, 2) > reqPixs) { inSampleSize *= 2; } return inSampleSize; } }