Android Bitmap Load decodeSampledBitmapFromBytesForCurrentScreen( byte[] res, Context ctxt)

Here you can find the source of decodeSampledBitmapFromBytesForCurrentScreen( byte[] res, Context ctxt)

Description

decode Sampled Bitmap From Bytes For Current Screen

Declaration

public static Bitmap decodeSampledBitmapFromBytesForCurrentScreen(
            byte[] res, Context ctxt) 

Method Source Code

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.media.ExifInterface;
import android.net.Uri;
import android.view.Display;
import android.view.View;

public class Main{
    private static final int MAX_WIDTH = 480;
    private static final int MAX_HEIGHT = 800;
    public static Bitmap decodeSampledBitmapFromBytesForCurrentScreen(
            byte[] res, Context ctxt) {
        Display display = DisplayUtils.getScreenDisplay(ctxt);
        Point size = new Point();
        display.getSize(size);/*from   w  ww .j a v  a  2s.c  om*/
        int width = size.x;
        int height = size.y;

        return decodeSampledBitmapFromBytes(res, width, height);
    }
    public static Bitmap decodeSampledBitmapFromBytes(byte[] res,
            int reqWidth, int reqHeight) {
        if (reqWidth > MAX_WIDTH)
            reqWidth = MAX_WIDTH;
        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, int reqWidth, int reqHeight)
  2. decodeFromFile(String path)
  3. decodeFromFile(String path, int reqWidth, int reqHeight)
  4. decodeSampledBitmapFileForSize(File f, int reqWidth, int reqHeight)
  5. decodeSampledBitmapFromBytes(byte[] res, int reqWidth, int reqHeight)
  6. decodeSampledBitmapFromResource(Resources res, int resId, int reqWidth, int reqHeight)
  7. decodeSampledBitmapFromUrl(URL url, int reqWidth, int reqHeight)
  8. decodeSampledBitmapStreamForSize(InputStream is, int reqWidth, int reqHeight)
  9. download(String url, String fileName)