6

I have looked at probably every SO article concering capturing the screen (screenshot, screendump) programmatically on Android, and they usually all end up with the same answer.

The problem with it is that it captures the View that you have specified, but it does NOT capture any Dialogs that may be "on top of" the "root view". This is the code I use, that fails to capture anything "on top":

Bitmap bitmap;
View v1 = findViewById(android.R.id.content);
v1.setDrawingCacheEnabled(true);
bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File path = Environment.getExternalStorageDirectory();
File file = new File(path, "myDump.jpg");

FileOutputStream outputStream;

try 
{
    outputStream = new FileOutputStream(file);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 10, outputStream);
    outputStream.flush();
    outputStream.close();
} 

catch (Exception e) 
{
  e.printStackTrace();
}

The question is: how can I capture the entire screen, including Dialogs that are on top? I am only interested in capturing the app that I am writing, not the home screen or anything like that, just anything that is on top of my root view.

I did read something about rooting, but I really hope that taking a complete screendump of the app Im writing cannot be impossible.

Ted
  • 19,727
  • 35
  • 96
  • 154
  • "The question is: how can I capture the entire screen, including Dialogs that are on top?" -- AFAIK, you can't, except via root methods. – CommonsWare Apr 18 '13 at 17:13
  • If the dialogs are your you should be able to use the getDrawingCache() method for them as well, then it would just be up to you to overlay the dialog.jpg on top of the activity.jpg. – FoamyGuy Apr 18 '13 at 17:25
  • Thx... so, I have to iterate "backwards", from the root and to the children, and overlay each view onto the root view image? *sigh* – Ted Apr 19 '13 at 06:47
  • Ted, were you able to get this working? – AndroidDev Jun 13 '13 at 15:53
  • Ted, have you tried getting all the children for the root view and then merge all the bitmaps onto a canvas? – AndroidDev Jun 13 '13 at 16:48
  • I have tried iterating through all the children views but that did not return the dialogs. Thinking further these are Android system dialogs and by capturing those we are testing the Android system dialogs and not our application, so this seems to be by design. If we root the device then we can capture the entire screen. – AndroidDev Jun 14 '13 at 08:55
  • possible duplicate of [Take screenshot with dialog](http://stackoverflow.com/questions/17393656/take-screenshot-with-dialog) – ben75 Apr 29 '15 at 10:17

3 Answers3

3

use this library .... it works great for me. https://github.com/jraska/Falcon

  // Saving screenshot to file
            Falcon.takeScreenshot(this, imageFile);
            // Take bitmap and do whatever you want
            Bitmap bitmap = Falcon.takeScreenshotBitmap(this);
MezzDroid
  • 560
  • 4
  • 11
1

It is possible, you need to draw all view roots to the bitmap. Try out this library: https://github.com/jraska/Falcon it can capture Dailogs to your screenshot.

Josef Raška
  • 1,291
  • 10
  • 8
0

This is working inside an opened DialogFragment.

View v1 = ((ViewGroup) (((MyActivity)getActivity()).findViewById(android.R.id.content)));
v1.setDrawingCacheEnabled(true);
Bitmap bitmapParent = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);

// dialogView is the inflated view of the DialogFragment
dialogView.setDrawingCacheEnabled(true);
Bitmap bitmapDialog = Bitmap.createBitmap(dialogView.getDrawingCache());
dialogView.setDrawingCacheEnabled(false);

Canvas canvas = new Canvas(bitmapParent);
Paint paint = new Paint(Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bitmapDialog, 0, 0, paint);   

// Activity and dialog captured!!
bitmapParent.compress(Bitmap.CompressFormat.PNG, 100, new FileOutputStream(new File(directory, name)));
ElYeante
  • 1,745
  • 21
  • 22