The android code implements the screenshot function and the android code screenshot

Source: Internet
Author: User

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 ;}}




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.