Android development through the view of the Getdrawingcache method can achieve the purpose of the screenshot, just missing the status bar!
Original interface
Screenshot of the resulting picture
Code implementation
1. Add permissions (in the Androidmanifest.xml file)
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
2. Add 1 button (activity_main.xml file)
<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools " android:layout_width=" match_parent " android:layout_height=" Match_parent " tools:context= ". Mainactivity "> <textview android:layout_width=" wrap_content " android:layout_height=" Wrap_ Content " android:layout_centerhorizontal=" true " android:layout_centervertical=" true " android:text= "@string/hello_world"/> <button android:id= "@+id/btn_save" android:layout_width= "Wrap_ Content " android:layout_height=" wrap_content " android:text=" screenshot " /></ Relativelayout>
3. Implement screenshot (Mainactivity.java file)
Package Com.example.androidtest;import Java.io.file;import Java.io.fileoutputstream;import android.os.Bundle; Import Android.os.environment;import android.app.activity;import android.graphics.bitmap;import android.view.Menu; Import Android.view.view;import Android.widget.button;public class Mainactivity extends Activity {@Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_ Main); Button btn = (button) This.findviewbyid (R.id.btn_save); Btn.setonclicklistener (new View.onclicklistener () {@ overridepublic void OnClick (View v) {screenshot ();}}); private void screenshot () {//Get screen view DView = GetWindow (). Getdecorview (); Dview.setdrawingcacheenabled (TRUE); Dview.builddrawingcache (); Bitmap bmp = Dview.getdrawingcache (); if (BMP! = null) {try {//Get built-in SD card path String Sdcardpath = Environment.getexternalstoragedirect Ory (). GetPath (); Picture file path String FilePath = SDcardPath + file.separator + "screenshot.png"; File File = new file (FilePath); FileOutputStream OS = new FileOutputStream (file); Bmp.compress (Bitmap.CompressFormat.PNG, MB, OS); Os.flush (); Os.close ();} catch (Exception e) {}}} @Overridepublic Boolean Oncreateoptionsmenu (Menu menu) {//Inflate the menu; this adds ite Ms to the action Bar if it is present.getmenuinflater (). Inflate (R.menu.activity_main, menu); return true;}}
Android Code for screenshot function