0

I want to take screenshot of my app where in one activity I have SurfaceView which holds tile map.

On click of a button, I execute screen capture code and show it in dialog along with an edittext and two buttons.

Using various posts on SO and on internet, I tried following code snippets:

Sample 1)

View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

Sample 2)

bitmap = Bitmap.createBitmap(v1.getWidth(),
v1.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
v1.draw(canvas);

Sample 3)

mDrawerLayout.setDrawingCacheEnabled(true);
mDrawerLayout.buildDrawingCache(true);
bitmap = Bitmap.createBitmap(mDrawerLayout.getDrawingCache());
mDrawerLayout.setDrawingCacheEnabled(false);

Sample 4)

bitmap = Bitmap.createBitmap(mDrawerLayout.getWidth(),
mDrawerLayout.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
mDrawerLayout.draw(canvas);

Now the closest I've reached to my requirement is with Sample 1 code itself. Check below images for the same.

Note that I'm not able to save actual image i.e. Image 1 on SD card properly. But at least using 1st code snippet, it's getting displayed in dialog box.

Also note that, for a normal screen (without having canvas), this code is working perfectly fine but when it comes to view with a SurfaceView, this fails.

Please find description for below images:

Image 1: Actual app screen - took this screenshot using device's feature of capturing screenshot in normal state

Image 2: Actual app screen - took this screenshot using device's feature of capturing screenshot when code for taking screenshot programmatically is executed

Image 3: Image saved on SD card after programmatically capturing screenshot

Image 1 Image 1


Image 2 Image 2

Image 3 Image 3

Am I doing something wrong? Any help appreciated.

GAMA
  • 5,958
  • 14
  • 79
  • 126
  • try enabling drawing cache for all views by setting it in the manifest although I'm kinda sure that I once read that when you enable drawing cache it stays enabled... – Ilja KO Sep 28 '15 at 08:07
  • Can you tell me how to **enable drawing cache for all views by setting it in the manifest**? – GAMA Sep 28 '15 at 08:38
  • Duplicate of http://stackoverflow.com/questions/27817577/ ? – fadden Sep 28 '15 at 15:51

0 Answers0