The android code implements the screenshot function and the android code screenshot
In android development, you can use the getDrawingCache method of View to Capture screenshots, but the status bar is missing!
Original Interface
Screenshots
Code Implementation
1. add permissions (in the AndroidManifest. xml file)
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
2. Add one 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 (save DInstanceState); 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 the screen View dView = getWindow (). getDecorView (); dView. setDrawingCacheEnabled (true); dView. buildDrawingCache (); Bitmap bmp = dView. getDrawingCache (); if (bmp! = Null) {try {// get the built-in SD card path String sdCardPath = Environment. getExternalStorageDirectory (). getPath (); // image File path String filePath = sdCardPath + File. separator + "screenshot.png"; File file = new File (filePath); FileOutputStream OS = new FileOutputStream (file); bmp. compress (Bitmap. compressFormat. PNG, 100, OS); OS. flush (); OS. close ();} catch (Exception e) {}}@ Overridepublic boolean onCreateOptionsMenu (Menu menu) {// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater (). inflate (R. menu. activity_main, menu); return true ;}}