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