decode Sample Bitmap From Resource - Android Graphics

Android examples for Graphics:Bitmap Resource

Description

decode Sample Bitmap From Resource

Demo Code


//package com.java2s;
import java.io.FileInputStream;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    public static Bitmap decodeSampleBitmapFromResource(Context context,
            String file, int reqWidth, int reqHeight) throws Exception {

        final BitmapFactory.Options options = new BitmapFactory.Options();
        FileInputStream in = context.openFileInput(file);
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        in.close();// ww  w .  j  a v a  2s  .c om
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);
        in = context.openFileInput(file);
        options.inJustDecodeBounds = false;
        Bitmap bitmap = BitmapFactory.decodeStream(in, null, options);
        in.close();
        return bitmap;

    }

    public static int calculateInSampleSize(BitmapFactory.Options options,
            int reqWidth, int reqHeight) {

        int height = options.outHeight;
        int inSampleSize = 1;

        if (height <= reqHeight) {
            inSampleSize = 1;
        } else {
            inSampleSize = height / reqHeight;
        }

        return inSampleSize;
    }
}

Related Tutorials