0

I have a CardView (with multiple items like imageView, textView, Buttons) in a RecyclerView. I want to get bitmap from this cardview. I tried a simple method of converting cardView into bitmap (convert a cardview to bitmap) but its not working properly. I only got the raw view of the card just like in xml without any updated items from a firebaseserver.

Ryan M
  • 18,333
  • 31
  • 67
  • 74

2 Answers2

1

CardView extend view, so you probably can convert it as any other view, try to pass the CardView into that function:

  public static Bitmap getBitmapFromView(View view) {
    //Define a bitmap with the same size as the view
    Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);
    //Bind a canvas to it
    Canvas canvas = new Canvas(returnedBitmap);
    //Get the view's background
    Drawable bgDrawable =view.getBackground();
    if (bgDrawable!=null) 
        //has background drawable, then draw it on the canvas
        bgDrawable.draw(canvas);
    else 
        //does not have background drawable, then draw white background on the canvas
        canvas.drawColor(Color.WHITE);
    // draw the view on the canvas
    view.draw(canvas);
    //return the bitmap
    return returnedBitmap;
}
Nirel
  • 1,855
  • 1
  • 15
  • 26
0

you are probably taking the screenshot of view before it is fully inflated. Try adding a 2 second delay or implement this on layout listener of the card view

Suhaib Roomy
  • 2,501
  • 1
  • 16
  • 22