Android Screen Capture

Source: Internet
Author: User

public class Screenshot {
Private Screenshotlistner Shotlistner;

Private Bitmap takescreenshot (activity activity) {
View is the view you need
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;
System.out.println (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 void Savepic (Bitmap b,string strFileName) {
FileOutputStream fos = null;
try {
FOS = new FileOutputStream (strFileName);
if (null! = FOS)
{
B.compress (Bitmap.CompressFormat.PNG, N, FOS);
Shotlistner.onsuccess (b);
Fos.flush ();
Fos.close ();
}
} catch (FileNotFoundException e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
}

Program entry
public void Shoot (Activity a) {
Savepic (Takescreenshot (a), "sdcard/xx.png");
}

public void Setshotlistner (Screenshotlistner shotlistner) {
if (shotlistner!=null) {
This.shotlistner = Shotlistner;
}
}

public interface screenshotlistner{
void onsuccess (Bitmap o);
}
}

Android screen

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.