1. Use the screenshot method inside the system ...
2.
Public classMainactivityextendsappcompatactivity {Private Static FinalString TAG = "Mainactivity"; View View1; intX=1; Private Static Final intRequest_media_projection = 1; PrivateMediaprojectionmanager Projectionmanager; BooleanScreencapture =false; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (R.layout.activity_main); } Public voidClick (View view) {Alertdialog.builder LocalBuilder=NewAlertdialog.builder ( This); Localbuilder.settitle (Custom List dialog box). SetIcon (R.mipmap.ic_launcher); View1=getlayoutinflater (). Inflate (R.layout.layout,NULL); Localbuilder.setview (Getlayoutinflater (). Inflate (R.layout.layout,NULL)); Localbuilder.setpositivebutton ("OK",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Paramanonymousdialoginterface,intparamanonymousint) { /*** * Operation **/ //Get ScreenScreencapture =true; X=1; TakeScreenshot2 (); Try{Thread.Sleep (500); } Catch(interruptedexception e) {e.printstacktrace (); }}). Setnegativebutton ("Cancel",NewDialoginterface.onclicklistener () { Public voidOnClick (Dialoginterface Paramanonymousdialoginterface,intparamanonymousint) { /*** * Operation **/}}). Create (). Show (); } Public voidTakeScreenshot2 () {Try{Projectionmanager=(Mediaprojectionmanager) Getsystemservice (Context.media_projection_service); Startactivityforresult (Projectionmanager.createscreencaptureintent (), Request_media _projection); } Catch(Exception e) {e.printstacktrace (); }} @Override Public voidOnactivityresult (intRequestcode,intResultCode, Intent data) { if(Requestcode = = Request_media_projection && screencapture==true) { /** * */ Try { Final intMwidth =Getwindowmanager (). Getdefaultdisplay (). GetWidth (); Final intMheight =Getwindowmanager (). Getdefaultdisplay (). GetHeight (); FinalImageReader Mimagereader = imagereader.newinstance (Mwidth, Mheight, pixelformat.rgba_8888, 2); Displaymetrics Metrics=NewDisplaymetrics (); Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics); intMscreendensity =metrics.densitydpi; FinalMediaprojection mprojection = projectionmanager.getmediaprojection (-1, data); LOG.I (TAG,"ONACTIVITYRESULT2:" +screencapture+ "* *" +x); FinalVirtualdisplay Virtualdisplay = Mprojection.createvirtualdisplay ("Screen-mirror", Mwidth, Mheight, mscreendensity, Displaymanager.virtual_display_flag_auto_mirror, Mimagereader.getsurface (),NULL,NULL); Mimagereader.setonimageavailablelistener (NewImagereader.onimageavailablelistener () {@Override Public voidonimageavailable (ImageReader reader) {if(x!=1){ return; } Else{ Try{log.i (TAG,"ONACTIVITYRESULT3:" +screencapture+ "* *" +x); /*** Remove--->mprojection.stop ();*/x=2; Mprojection.stop (); Image Image=Mimagereader.acquirelatestimage (); FinalImage.plane[] Planes =Image.getplanes (); FinalBytebuffer buffer = Planes[0].getbuffer (); intOffset = 0; intPixelstride = Planes[0].getpixelstride (); intRowstride = Planes[0].getrowstride (); introwpadding = Rowstride-pixelstride *Mwidth; Bitmap Bitmap= Bitmap.createbitmap (mwidth + rowpadding/pixelstride, Mheight, Bitmap.Config.ARGB_8888); Bitmap.copypixelsfrombuffer (buffer); Image.close (); SimpleDateFormat DateFormat=NewSimpleDateFormat ("Yyyy_mm_dd_hh_mm_ss"); String strdate= Dateformat.format (Newjava.util.Date ()); /*** Save Path ...*/String pathimage= Environment.getexternalstoragedirectory (). GetPath () + "/"; /*** Name ...*/String nameimage= pathimage + strdate + ". png"; if(Bitmap! =NULL) { Try{File Fileimage=NewFile (nameimage); if(!fileimage.exists ()) {Fileimage.createnewfile (); } FileOutputStream out=NewFileOutputStream (fileimage); if(Out! =NULL) {bitmap.compress (Bitmap.CompressFormat.PNG,100, out); Out.flush (); Out.close (); Toast.maketext (mainactivity. This, "Picture saved successfully!", Toast.length_short). Show (); Intent Media=NewIntent (Intent.action_media_scanner_scan_file); Uri Contenturi=Uri.fromfile (fileimage); Media.setdata (Contenturi); Getapplicationcontext (). Sendbroadcast (media); Screencapture=false; } } Catch(FileNotFoundException e) {e.printstacktrace (); } Catch(IOException e) {e.printstacktrace (); } } Else{toast.maketext (mainactivity). This, "Cannot get phone ' s screen", Toast.length_short). Show (); } } Catch(Exception e) {e.printstacktrace (); } } } }, NULL); } Catch(Exception e) {e.printstacktrace (); } /** * */ } }}
android< including alertdiaog>