Android Bitmap Load decodeSampledBitmapFromBytes(byte[] res, int reqWidth, int reqHeight)

Here you can find the source of decodeSampledBitmapFromBytes(byte[] res, int reqWidth, int reqHeight)

Description

decode Sampled Bitmap From Bytes

Declaration

public static Bitmap decodeSampledBitmapFromBytes(byte[] res,
            int reqWidth, int reqHeight) 

Method Source Code

//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class Main {
    private static final int MAX_WIDTH = 480;
    private static final int MAX_HEIGHT = 800;

    public static Bitmap decodeSampledBitmapFromBytes(byte[] res,
            int reqWidth, int reqHeight) {
        if (reqWidth > MAX_WIDTH)
            reqWidth = MAX_WIDTH;//from w  w w  .jav a2  s  . c o  m
        if (reqHeight > MAX_HEIGHT)
            reqHeight = MAX_HEIGHT;

        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        options.inPurgeable = true;
        options.inInputShareable = true;
        BitmapFactory.decodeByteArray(res, 0, res.length, options);

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

        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeByteArray(res, 0, res.length, options);
    }

    private static int calculateInSampleSize(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);
        }

        return inSampleSize;
    }
}

Related

  1. decodeBitmap(Resources res, int resId)
  2. decodeBitmap(Resources res, int resId, int reqWidth, int reqHeight)
  3. decodeFromFile(String path)
  4. decodeFromFile(String path, int reqWidth, int reqHeight)
  5. decodeSampledBitmapFileForSize(File f, int reqWidth, int reqHeight)
  6. decodeSampledBitmapFromBytesForCurrentScreen( byte[] res, Context ctxt)
  7. decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
  8. decodeSampledBitmapFromUrl(URL url, int reqWidth, int reqHeight)
  9. decodeSampledBitmapStreamForSize(InputStream is, int reqWidth, int reqHeight)