Android development through the view of the Getdrawingcache method can achieve the purpose of screenshots, but the lack of status bar!
Original interface
Screenshot to get the picture
Code implementation
1. Add permissions (androidmanifest.xml file)
Copy Code code as follows:
<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. Implementation of screenshots (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 {@Override protected void onCreate (Bundle savedinstancestate) {Super.onc
Reate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Button btn = (button) This.findviewbyid (R.id.btn_save);
Btn.setonclicklistener (New View.onclicklistener () {@Override public 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 {//Gets the built-in SD card path String Sdcardpath = Environment.getexternalstoragedirectory (). Get
Path (); 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 (); The catch (Exception e) {}}} @Override public boolean Oncreateoptionsmenu (Menu menu) {//inflate the menu; th
is adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
}
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.