get Bitmap By Id from resource - Android Graphics

Android examples for Graphics:Bitmap Resource

Description

get Bitmap By Id from resource

Demo Code


//package com.java2s;

import java.io.InputStream;
import android.content.Context;
import android.graphics.Bitmap;

import android.graphics.BitmapFactory;

public class Main {

    public static Bitmap getBitmapById(Context context, int resId) {
        if (context == null) {
            return null;
        }/*from   w  w w .  ja  v a  2 s . com*/

        return BitmapFactory.decodeResource(context.getResources(), resId);
    }

    @SuppressWarnings("deprecation")
    public static Bitmap decodeResource(Context context, int resourseId) {
        BitmapFactory.Options opt = new BitmapFactory.Options();
        opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
        opt.inPurgeable = true;
        opt.inInputShareable = true; // ?? inPurgeable???true????????

        // ?????????
        InputStream is = context.getResources().openRawResource(resourseId);

        return BitmapFactory.decodeStream(is, null, opt); // decodeStream????JNI>>nativeDecodeAsset()??????decode???????java??createBitmap???????java????
    }
}

Related Tutorials