Mode one, call the getandsavecurrentimage () method to
/** * Gets and saves the current screen of */private void Getandsavecurrentimage () {//build Bitmapwindowmanager WindowManager = Getwindowmanager ();D Isplay display = Windowmanager.getdefaultdisplay (); int w = Display.getwidth (); int h = display.getheight (); Bitmap Bmp = Bitmap.createbitmap (W, H, config.argb_8888);//Get screen view Decorview = This.getwindow (). Getdecorview (); Decorview.setdrawingcacheenabled (TRUE); BMP = Decorview.getdrawingcache ();//Picture storage path String savepath = Getsdcardpath () + "/test/screenimages";//Save Bitmaplog.d (" Debug "," Savepath = "+savepath); try {file path = new file (savepath);//File string filepath = Savepath +"/screen_1.png "; LOG.D ("Debug", "filepath =" +filepath); File File = new file (filepath), if (!path.exists ()) {LOG.D ("Debug", "Path is NOT exists");p ath.mkdirs ();} if (!file.exists ()) {LOG.D ("Debug", "File Create new"); File.createnewfile ();} FileOutputStream fos = Null;fos = new FileOutputStream (file); if (null! = FOS) {bmp.compress (Bitmap.CompressFormat.PNG, 90 , FOS); Fos.flush (); Fos.close (); Toast.maketext (ThiS, "The screenshot file has been saved to the sdcard/screenimages/directory", Toast.length_long). Show (); LOG.D ("Debug", "Save OK");}} catch (Exception e) {e.printstacktrace ();}} /** * Get SDcard directory path function * * @return */private String Getsdcardpath () {File Sdcarddir = null;//to determine if SDcard exists Boolean Sdcardex ist = environment.getexternalstoragestate (). Equals (Android.os.Environment.MEDIA_MOUNTED); if (sdcardexist) { Sdcarddir = Environment.getexternalstoragedirectory ();} return sdcarddir.tostring ();}Mode two, call methodscreenshot
Privatevoid screenshot (View v) {SimpleDateFormat sdf = new SimpleDateFormat ("Yyyy-mm-dd_hh-mm-ss", locale.us); String fname = "/sdcard/" + Sdf.format (new Date ()) + ". png"; LOG.D ("Debug", "fname =" + fname); View view = V.getrootview (); view.setdrawingcacheenabled (true); View.builddrawingcache (); Bitmap Bitmap = View.getdrawingcache (), if (Bitmap! = null) {FileOutputStream out = null;try {out = new FileOutputStream (fn AME); LOG.D ("Debug", "FileOutputStream");} catch (FileNotFoundException e) {//TODO auto-generated catch Blocke.printstacktrace (); LOG.D ("Debug", "Bitmap is Error");} Bitmap.compress (Bitmap.CompressFormat.PNG, N, out); LOG.D ("Debug", "Bitmap compress OK");} else {log.d ("Debug", "Bitmap is null");}}
Remember to add permission settings
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
Android-Mobile phone screenshot