Android screenshot function Implementation code _android

Source: Internet
Author: User

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.

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.