get Bitmap from image path by width and height - Android Graphics

Android examples for Graphics:Bitmap Size

Description

get Bitmap from image path by width and height

Demo Code


//package com.book2s;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.ThumbnailUtils;

public class Main {
    public static Bitmap getBitmap(String imagePath, int width, int height) {
        Bitmap bitmap = null;// w  w w . j a  va2s .c o  m
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
 
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        options.inJustDecodeBounds = false; 
        int h = options.outHeight;
        int w = options.outWidth;
        int beWidth = w / width;
        int beHeight = h / height;
        int be = 1;
        if (beWidth < beHeight) {
            be = beWidth;
        } else {
            be = beHeight;
        }
        if (be <= 0) {
            be = 1;
        }
        options.inSampleSize = be;
        bitmap = BitmapFactory.decodeFile(imagePath, options);
        bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height,
                ThumbnailUtils.OPTIONS_RECYCLE_INPUT);
        return bitmap;
    }
}

Related Tutorials