image Resource To Bitmap - Android Media

Android examples for Media:Picture

Description

image Resource To Bitmap

Demo Code


//package com.book2s;

import android.content.Context;
import android.content.res.Resources;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

public class Main {
    public static Bitmap imageResourceToBitmap(Context c, Resources res,
            int id, int maxDim) {
        Bitmap bmp = null;/*from   www.  j av a2 s  .  c  o m*/
        BitmapFactory.Options opts = new BitmapFactory.Options();
        opts.inJustDecodeBounds = true;

        // compute the smallest size bitmap we need to read
        BitmapFactory.decodeResource(res, id, opts);

        int w = opts.outWidth;
        int h = opts.outHeight;
        int s = 1;
        while (true) {
            if ((w / 2 < maxDim) || (h / 2 < maxDim)) {
                break;
            }
            w /= 2;
            h /= 2;
            s *= 2;
        }

        // scale and read the data
        opts.inJustDecodeBounds = false;
        opts.inSampleSize = s;
        bmp = BitmapFactory.decodeResource(res, id, opts);

        return bmp;
    }
}

Related Tutorials