This article describes how to implement the ability to intercept the current screen on an Android phone or tablet, and save the intercepted screen under a directory folder in SDcard. The
implemented code is as follows:
/** * Capture and save screenshot of current screen/private void Getandsavecurrentimage () {//1. Build bitmap WindowManager WindowManager = Getwin
Dowmanager ();
Display display = Windowmanager.getdefaultdisplay ();
int w = display.getwidth ();
int h = display.getheight ();
Bitmap Bmp = Bitmap.createbitmap (W, H, config.argb_8888);
2. Get screen View Decorview = This.getwindow (). Getdecorview ();
Decorview.setdrawingcacheenabled (TRUE);
BMP = Decorview.getdrawingcache ();
String Savepath = Getsdcardpath () + "/andydemo/screenimage";
3. Save Bitmap try {file path = new file (Savepath);
File String filepath = savepath + "/screen_1.png";
File 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, N, FOS);
Fos.flush ();
Fos.close (); Toast.maketext (MconteXT, "screenshot file has been saved to sdcard/andydemo/screenimage/", Toast.length_long). Show ();
} catch (Exception e) {e.printstacktrace ();
/** * Gets sdcard directory path feature * @return/private String Getsdcardpath () {File sdcarddir = null; Determine if sdcard exists boolean sdcardexist = Environment.getexternalstoragestate (). Equals (android.os.Environment.MEDIA_
mounted);
if (sdcardexist) {sdcarddir = Environment.getexternalstoragedirectory ();
return sdcarddir.tostring (); }
Because you want to operate on sdcard, don't forget to assign the read and write permissions to SDcard in the Manifest.xml file:
I hope this article will help you learn about Android software programming.