Android screenshot capture Bullet Box Alertdialog

Source: Internet
Author: User

1: Effect

2: Permissions

   <USES-SDK        android:minsdkversion= "android:targetsdkversion="        />    <uses-permission Android:name= "Android.permission.READ_EXTERNAL_STORAGE"/>    <uses-permission android:name= " Android.permission.MOUNT_UNMOUNT_FILESYSTEMS "        tools:ignore=" protectedpermissions "/>    < Uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>

  

3: implementation

public class Mainactivity extends Appcompatactivity {private static final String TAG = "Taga";    int x = 1;    private static final int request_media_projection = 1;    Private Mediaprojectionmanager Projectionmanager;    Boolean screencapture = false; Handler Mhandler = new Handler () {@TargetApi (build.version_codes.            LOLLIPOP) @Override public void Handlemessage (Message msg) {super.handlemessage (msg);        TakeScreenshot2 ();    }    };        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);        Button btn = Findviewbyid (R.ID.BTN);                Btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (View v) {                X=1;                Showalertdialog ();            Mhandler.sendemptymessagedelayed (100,1000);    }        }); } @TargetApi (Build.version_codes. LOLLIPOP)    @RequiresApi (API = Build.version_codes. KITKAT) @Override protected void Onactivityresult (int requestcode, int resultcode, Intent data) {if (Request Code = = request_media_projection) {try {final int mwidth = Getwindowmanager (). GETDEFAULTDISPL                Ay (). getwidth ();                Final int mheight = Getwindowmanager (). Getdefaultdisplay (). GetHeight ();                Final ImageReader Mimagereader = imagereader.newinstance (Mwidth, Mheight, pixelformat.rgba_8888, 2);                Displaymetrics metrics = new Displaymetrics ();                Getwindowmanager (). Getdefaultdisplay (). Getmetrics (metrics);                int mscreendensity = metrics.densitydpi;                Final Mediaprojection mprojection = projectionmanager.getmediaprojection ( -1, data); Final Virtualdisplay Virtualdisplay = Mprojection.createvirtualdisplay ("Screen-mirror", Mwidth, MHe ight, Mscreendensity, displaymanager.virtual_display_flag_auto_mIrror, Mimagereader.getsurface (), NULL, NULL);                    Mimagereader.setonimageavailablelistener (New Imagereader.onimageavailablelistener () {@Override                            public void onimageavailable (ImageReader reader) {if (x! = 1) {                        Return } else {try {/** * minus---&GT;MP Rojection.stop ();                                There will be a constant phenomenon */x = 2;                                Mprojection.stop ();                                Image image = Mimagereader.acquirelatestimage ();                                Final image.plane[] planes = Image.getplanes ();                                Final Bytebuffer buffer = Planes[0].getbuffer ();                                int offset = 0; int pixelstride = Planes[0].getpixelstride ();                                int rowstride = Planes[0].getrowstride ();                                int rowpadding = Rowstride-pixelstride * mwidth;                                Bitmap Bitmap = bitmap.createbitmap (Mwidth + rowpadding/pixelstride, mheight, Bitmap.Config.ARGB_8888);                                Bitmap.copypixelsfrombuffer (buffer);                                Image.close (); Save path String pathimage = Environment.getexternalstoragedirectory (). GetPath () + File.separ                                Ator + "AA" + file.separator;                                String Nameimage = pathimage+ "example. png";  if (bitmap! = null) {try {File fileimage = new                                        File (Nameimage);                                if (!fileimage.exists ()) {fileimage.createnewfile ();        } FileOutputStream out = new FileOutputStream (fileimage); if (out! = null) {bitmap.compress (Bitmap.compressformat .                                            PNG, +, out);                                            Out.flush ();                                            Out.close ();                                            Toast.maketext (Mainactivity.this, "Picture saved successfully!", Toast.length_short). Show ();                                            Intent media = new Intent (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 (); }}}/** * Function call */@RequiresApi (API = Build.version_codes. LOLLIPOP) public void TakeScreenshot2 () {try {Projectionmanager = (Mediaprojectionmanager) getsyste            Mservice (Context.media_projection_service); StartactivityforresUlt (Projectionmanager.createscreencaptureintent (), request_media_projection);        } catch (Exception e) {e.printstacktrace ();        }} public void Showalertdialog () {Alertdialog.builder localbuilder = new Alertdialog.builder (this);        Localbuilder.settitle ("Simple dialog box");        Localbuilder.seticon (R.mipmap.ic_launcher); Localbuilder.setmessage ("hint message?        "); Localbuilder.setpositivebutton ("OK", new Dialoginterface.onclicklistener () {public void OnClick (DIALOGINTERFAC            e paramanonymousdialoginterface, int paramanonymousint) {/** * OK operation * */        }        }); Localbuilder.setnegativebutton ("Cancel", new Dialoginterface.onclicklistener () {public void OnClick (DIALOGINTERFAC            e paramanonymousdialoginterface, int paramanonymousint) {/** * OK operation * */        }        });        /*** * Set the Click Back key does not disappear * */localbuilder.setcancelable (FALSE). Create ();    Localbuilder.show (); }}

  

Android Intercept Bullet frame Alertdialog

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.