The screenshot function is used in recent projects. After searching for information, it is summarized as follows:
Method 1:
/*** Simple screenshot method ** @ Param v view * @ Param filepath save path */private void getscreenhot (view V, string filepath) {try {Bitmap bitmap = bitmap. createbitmap (v. getwidth (), V. getheight (), config. argb_8888); canvas = new canvas (); canvas. setbitmap (Bitmap); V. draw (canvas); try {fileoutputstream Fos = new fileoutputstream (filepath); bitmap. compress (compressformat. PNG, 90, FOS);} catch (filenotfoundexception e) {log. E ("errormsg", E. tostring (); toast. maketext (this, "path not found", 200 ). show () ;}} catch (exception e) {log. E ("errormsg", E. tostring (); toast. maketext (this, "failed", 200 ). show ();}}
Call getscreenhot (View) getwindow (). getdecorview (), "/sdcard/pic1.png ");
Method 2:
/*** Get and save the current screen */private void savecurrentimage () {// create bitmap windowmanager = getwindowmanager (); display = windowmanager. getdefadisplay display (); int W = display. getwidth (); int H = display. getheight (); bitmap BMP = bitmap. createbitmap (W, H, config. argb_8888); // obtain the screen view decorview = This. getwindow (). getdecorview (); decorview. setdrawingcacheenabled (true); BMP = decorview. getd Rawingcache (); string savepath = getsdcardpath () + "/screenimage"; // The storage is bitmap try {simpledateformat SDF = new simpledateformat ("yyyy-MM-dd_HH-mm-ss", locale. china); File Path = new file (savepath); // file string filepath = savepath + "/" + SDF. format (new date () + ". PNG "; file = new file (filepath); If (! Path. exists () {path. mkdirs ();} If (! File. exists () {file. createnewfile ();} fileoutputstream Fos = NULL; Fos = new fileoutputstream (File); If (null! = FOS) {BMP. compress (bitmap. compressformat. PNG, 100, FOS); FOS. flush (); FOS. close (); toast. maketext (this, "The screenshot file has been saved to sdcard/screenimage/", toast. length_long ). show () ;}} catch (exception e) {e. printstacktrace () ;}}/*** get sdcard directory * @ return */private string getsdcardpath () {file sdcarddir = NULL; // determine whether the sdcard has Boolean sdcardexist = environment. getexternalstoragestate (). equals (Android. OS. environment. media_mounted); If (sdcardexist) {sdcarddir = environment. getexternalstoragedirectory ();} return sdcarddir. tostring ();}