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
Am I doing something wrong? Any help appreciated.