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