android Window screenshot

Source: Internet
Author: User

Steps:

The first step: Get the Bitmap object to save the screen image;

The second step gets the height of the status bar;

The third step gets the height of the screen image;

The fourth step creates a new bitmap object and intercepts other areas except the status bar;

Fifth step to save the screen image to the root of the SD card


The code is as follows:

Package Com.example.activityproperty;import Java.io.file;import Java.io.fileoutputstream;import Java.io.ioexception;import Android.annotation.suppresslint;import Android.app.activity;import Android.graphics.bitmap;import Android.graphics.point;import Android.graphics.rect;import Android.os.Bundle; Import Android.os.handler;import Android.view.view;import Android.widget.toast;public class MainActivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {//TODO auto-generated method Stubsuper.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main);} The @SuppressLint ("Newapi") public void Onclick_capturescreen (view view) {//uses a deferred run to prevent the screen image from being captured when the button is clicked without lifting the new Handler (). Postdelayed (New Runnable () {@SuppressLint ("Newapi") @Overridepublic void Run () {//Gets the top-level view object of the window view V = GetWindow (). Getdecorview (); v.setdrawingcacheenabled (true); V.builddrawingcache ();//First step: Get the Bitmap object that holds the screen image bitmap Srcbitmap = V.getdrawingcache (); Rect frame = new rect ()//Decorview is the topmost v in the windowIew, you can get decorview from window, and Decorview have a getwindowvisibledisplayframe method to get to the area that the program displays, including the title bar, but not the status bar. GetWindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);//The second step gets the height of the status bar int statusheight = frame.top;//the third step Gets the height of the screen image point outsize = new Point (); Getwindowmanager (). Getdefaultdisplay (). GetSize (outsize); int width = outsize.x; int height = outsize.y;//The fourth step creates a new bitmap object and intercepts other areas except the status bar bitmap bitmap = Bitmap.createbitmap (srcbitmap, 0, Statusheight,wi DTH, height-statusheight); V.destroydrawingcache (); FileOutputStream fos = null;try {File File = File.createtempfile ("Capture", ". jpg", New File ("/sdcard")); fos = new FILEOUTP Utstream (file); if (null! = FOS) {//Fifth step: Save the screen image to the root directory of the SD card bitmap.compress (Bitmap.CompressFormat.PNG, N, Fos); Fos.flush ( ); Toast.maketext (Mainactivity.this, "Save succeeded" + file.getname (), 0). Show (); Else{toast.maketext (Mainactivity.this, "failed", 0). Show (); Fos.close ();} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}}, 2000);}}

Note the permission to operate the SD needs to be added.

android Window screenshot

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.