Use this method below in android4.0:
Private void cutscreen () {view v = getlayoutinflater (). inflate (R. layout. activity_main, null); // enable image cache v. setdrawingcacheenabled (true); // v. builddrawingcache (); // measure the View Size v. measure (measurespec. makemeasurespec (0, measurespec. unspecified), measurespec. makemeasurespec (0, measurespec. unspecified); // sends the position and size to the view and all its subviews/* view v = This. getwindow (). getdecorview (); V. setdrawingcacheenabled (true); V. builddrawingcache (); */v. layout (0, 0, V. getmeasuredwidth (), V. getmeasuredheight (); loghelper. logv ("" + v. getmeasuredwidth () + "" + v. getmeasuredheight (); try {Bitmap bitmap = v. getdrawingcache (); fileoutputstream Fos = new fileoutputstream ("/sdcard/test.png"); bitmap. compress (compressformat. PNG, 100, FOS); FOS. close (); loghelper. logE ("screenshot succeeded");} catch (exception e) {// todo auto-generated catch blockloghelper. logE ("screenshot failed +" + E. getmessage (); E. printstacktrace ();}}
For Android 4.0 and later versions, you can use the following code:
Private void cutscreen () {/* view v = getlayoutinflater (). inflate (R. layout. activity_main, null); // enable image cache v. setdrawingcacheenabled (true); // v. builddrawingcache (); // measure the View Size v. measure (measurespec. makemeasurespec (0, measurespec. unspecified), measurespec. makemeasurespec (0, measurespec. unspecified); * // sends the position and size to the view and all its sub viewviews v = This. getwindow (). getdecorview (); V. setdrawingcacheenabled (true); V. builddrawingcache (); V. layout (0, 0, V. getmeasuredwidth (), V. getmeasuredheight (); loghelper. logv ("" + v. getmeasuredwidth () + "" + v. getmeasuredheight (); try {Bitmap bitmap = v. getdrawingcache (); fileoutputstream Fos = new fileoutputstream ("/sdcard/test.png"); bitmap. compress (compressformat. PNG, 100, FOS); FOS. close (); loghelper. logE ("screenshot succeeded");} catch (exception e) {// todo auto-generated catch blockloghelper. logE ("screenshot failed +" + E. getmessage (); E. printstacktrace ();}}