Martin Seal Martin Seal - 1 year ago 34
Android Question

converting a drawable to a bitmap giving me an error

i want to convert a drawable from an object to a bitmap, but when i try to use this

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(),

it gives me an error wrong second argument expecting 'int' found 'drawable' but everything i read says it should take a drawable. When i begin to write cardWriter.getCardImage() android studios code completion says its a drawable,
this is from my CardWriters class

public CardWriter(Drawable cardImage, String cardEmotion, String
this.cardImage = cardImage;
this.cardEmotion = cardEmotion;
this.speechText = speechText;


i think it may be my context thats wrong but i dont see how as its just a method that i pass the context too, heres the full thing

public void insertCardDetails(Context context, CardWriter cardWriter) {
ContentValues cv = new ContentValues();
Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), cardWriter.getCardImage());
cv.put(CARD_PHOTO, Utility.getBytes(bitmap));
cv.put(CARD_NAME, cardWriter.getCardEmotion());
cv.put(CARD_SPEECH, cardWriter.getSpeechText());
IconsDB.insert(EMPLOYEES_TABLE, null, cv);

what am i not doing?

Answer Source

BitmapFactory.decodeResource expects a drawable id for the second parameter.

You should do:

Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), R.drawable.your_drawable_id);

Or if you don't know the id:

Bitmap bitmap = ((BitmapDrawable)cardWriter.getCardImage()).getBitmap;