Android How to - Get Drawable to a Bitmap








Question

We would like to know how to get Drawable to a Bitmap.

Answer

/*from   w w  w.ja v a 2  s.com*/
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;

public class Main {
  public static Bitmap getDrawableToBitmap(int res_id, Context context)
  {
    BitmapDrawable drawable = (BitmapDrawable) context.getResources().getDrawable(res_id);
    Bitmap bitmap = drawable.getBitmap();
    
    return bitmap;
  }
}