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 ans
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)));