3

I've read pretty much every post on this topic, but none of them seem to help.

I'm trying to capture a screen shot of the current screen. For this I'm using getDrawingCache. Here's my code:

mRootView.get().setDrawingCacheEnabled(true);
mRootView.get().buildDrawingCache();
Bitmap screenShot = Bitmap.createBitmap(mRootView.get().getDrawingCache());
mRootView.get().setDrawingCacheEnabled(false);

The resulting bitmap is always black.

mRootView is a weak-reference to the drawer layout, which is my root view.

Here's what I've tried:

  1. Adding a measure and layout call (although this shouldn't be needed since this code runs when a button is pressed, so the view should already be layed out).
  2. Setting the layer type to LAYER_TYPE_NONE before calling setDrawingCacheEnabled(true)
  3. Using a different view as the root view (for example, a ViewPager inside the DrawerLayout).

Nothing seems to work and I've run out of ideas.

Gil Moshayof
  • 16,633
  • 4
  • 47
  • 58

2 Answers2

1

You can draw your view to canvas-

pass your main layout reference to this method-

 Bitmap file = save(layout);

 Bitmap save(View v)
   {
    Bitmap b = Bitmap.createBitmap(v.getWidth(), v.getHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(b);
    v.draw(c);
    return b;
   }
T_V
  • 17,440
  • 6
  • 36
  • 48
  • 1
    Wow, of course! It's so simple! I don't understand why this is the 1st post I found which shows how to do this in such a simple way. Thank you very much! It works perfectly! – Gil Moshayof Jan 22 '15 at 16:43
0

If above solution not working so just follow this link might be helpful convert view to bitmap

Sagar Jethva
  • 986
  • 12
  • 26