Android screenshot Save instance code for PNG picture _android

Source: Internet
Author: User

Copy Code code as follows:

Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;

Import android.app.Activity;
Import Android.graphics.Bitmap;
Import Android.graphics.Rect;
Import Android.util.Log;
Import Android.view.View;

public class Screenshot {
Gets the screenshot of the specified activity and saves it to the PNG file
private static Bitmap Takescreenshot (activity activity) {
View is the view you need to capture.
View view = Activity.getwindow (). Getdecorview ();
View.setdrawingcacheenabled (TRUE);
View.builddrawingcache ();
Bitmap B1 = View.getdrawingcache ();

Get status bar height
Rect frame = new Rect ();
Activity.getwindow (). Getdecorview (). Getwindowvisibledisplayframe (frame);
int statusbarheight = Frame.top;
LOG.I ("TAG", "" + statusbarheight);

Get screen length and height
int width = Activity.getwindowmanager (). Getdefaultdisplay (). GetWidth ();
int height = Activity.getwindowmanager (). Getdefaultdisplay ()
. GetHeight ();
Remove title bar
Bitmap B = Bitmap.createbitmap (B1, 0, 25, 320, 455);
Bitmap B = Bitmap.createbitmap (b1, 0, statusbarheight, width, height
-Statusbarheight);
View.destroydrawingcache ();
return b;
}

Save to SDcard
private static void Savepic (Bitmap B, String strFileName) {
FileOutputStream fos = null;
try {
FOS = new FileOutputStream (strFileName);
if (null!= fos) {
B.compress (Bitmap.CompressFormat.PNG, N, FOS);
Fos.flush ();
Fos.close ();
}
catch (FileNotFoundException e) {
E.printstacktrace ();
catch (IOException e) {
E.printstacktrace ();
}
}

Program entry
public static void shoot (activity a) {
Screenshot.savepic (Screenshot.takescreenshot (a), "sdcard/xx.png");
}
}



It should be noted that the shoot method can only be invoked after the view has been loaded.
or in
Copy Code code as follows:

@Override
public void Onwindowfocuschanged (Boolean hasfocus) {
TODO auto-generated Method Stub
Super.onwindowfocuschanged (Hasfocus);
Screenshot.shoot (this);
Called in

Related Article

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.