Android window screenshot

Source: Internet
Author: User

Android window screenshot

Steps:

Step 1: Obtain the Bitmap object that saves the screen image;

Step 2: Obtain the height of the status bar;

Step 3: Obtain the screen image height;

Step 4: Create a New Bitmap object and intercept other regions except the status bar;

Step 5 Save the screen image to the root directory 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 MainActivit Y extends Activity {@ Overrideprotected void onCreate (Bundle savedInstanceState) {// TODO Auto-generated method stubsuper. onCreate (savedInstanceState); setContentView (R. layout. activity_main) ;}@ SuppressLint ("NewApi") public void onClick_CaptureScreen (View view) {// use delayed running to prevent the screen image from being captured even when the button is clicked. new Handler (). postDelayed (new Runnable () {@ SuppressLint ("NewApi") @ Overridepublic void run () {// obtain the window's top-level view object Vi Ew v = getWindow (). getDecorView (); v. setDrawingCacheEnabled (true); v. buildDrawingCache (); // Step 1: Obtain the Bitmap object of the saved screen image. Bitmap srcBitmap = v. getDrawingCache (); Rect frame = new Rect (); // decorView is the top-level view in the window, which can be obtained from the window, then, decorView has a getWindowVisibleDisplayFrame method to obtain the display area of the program, including the title bar, but not the status bar. GetWindow (). getDecorView (). getWindowVisibleDisplayFrame (frame); // The height of the status bar int statusHeight = frame in step 2. top; // Step 3: Obtain the Screen Image Height Point outSize = new Point (); getWindowManager (). getdefadisplay display (). getSize (outSize); int width = outSize. x; int height = outSize. y; // create a new Bitmap object in step 4 and intercept Bitmap = bitmap in other regions except the status bar. createBitmap (srcBitmap, 0, statusHeight, width, height-statusHeight); v. destroyDrawingCache (); FileOutputStream fos = null; try {File file = File. createTempFile ("capture ",". jpg ", new File ("/sdcard "); fos = new FileOutputStream (file); if (null! = Fos) {// Step 5: Save the screen image to the root directory bitmap of the SD card. compress (Bitmap. compressFormat. PNG, 90, fos); fos. flush (); Toast. makeText (MainActivity. this, "saved successfully" + 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 that permissions for operations on SD must be added.

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.