Android Screenshots < includes alertdiaog>

Source: Internet
Author: User
Tags dateformat

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>

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.