将可绘制对象转换为位图给我一个错误

converting a drawable to a bitmap giving me an error

我想将可绘制对象从对象转换为位图,但是当我尝试使用它时

    Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), 
    cardWriter.getCardImage());

它给了我一个错误,错误的第二个参数期望 'int' 找到 'drawable' 但我读到的所有内容都说它应该采用可绘制对象。当我开始编写 cardWriter.getCardImage() android 工作室代码完成时说它是一个可绘制的, 这是我的 CardWriters class

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

} 

我认为这可能是我的上下文错误,但我不明白它是如何传递上下文的,这里是完整的东西

 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);
}

我没有做什么?

BitmapFactory.decodeResource 需要第二个参数的可绘制 ID。

你应该这样做:

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

或者如果您不知道 ID:

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