How to capture everything currently on screen including Dialogs

前端 未结 3 1334
天命终不由人
天命终不由人 2021-01-12 23:30

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

3条回答
  •  一生所求
    2021-01-13 00:11

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

提交回复
热议问题