[Code] Android screenshot function realization

When you use the mobile phone, sometimes there is a need, you need to put the mobile phone screen capture and save it to a storage card, since the user will have such needs, we developers need to how to achieve this function? In this paper, we provide a function, this function can help you achieve your screenshot function, in direct call places you need this function can be the current screen in the form of pictures saved to SDCard, function as follows:
/**
* Access and save the current screen.
*/
private void GetandSaveCurrentImage()
{
//Construction of Bitmap
WindowManager windowManager = getWindowManager();
Display display = windowManager.getDefaultDisplay();
int w = display.getWidth();
int h = display.getHeight();
Bitmap Bmp = Bitmap.createBitmap( w, h, Config.ARGB_8888 );
//Access to the screen
View decorview = this.getWindow().getDecorView();
decorview.setDrawingCacheEnabled(true);
Bmp = decorview.getDrawingCache();
//Picture storage path
String SavePath = getSDCardPath()+"/Demo/ScreenImages";
//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, 90, fos);
fos.flush();
fos.close();
Toast.makeText(mContext, "Screenshot file has been saved to the SDCard/ScreenImages/ directory",Toast.LENGTH_LONG).show();
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* The directory path function for SDCard
* @return
*/
private String getSDCardPath(){
File sdcardDir = null;
//To determine whether SDCard exists
boolean sdcardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
if(sdcardExist){
sdcardDir = Environment.getExternalStorageDirectory();
}
return sdcardDir.toString();
}

The operation of the SDCard in the AndroidManifest.xml file Fu write permission to the SDCard read, as follows:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


More Android Technology Group: 278744577, welcome to add validation code: CSDN, don't write verification shall not pass.~

Started by Hogan at December 12, 2016 - 12:12 PM

Mark learning.!~

Posted by Kate at December 19, 2016 - 12:42 PM

This method seems only intercept their application screen.

Posted by Tommy at December 31, 2016 - 1:32 PM

If this is true, is not available the interception of any interface

Posted by Melissa at January 05, 2017 - 2:53 PM

It will error, Bmp is null.

Posted by Lewis at January 13, 2017 - 1:55 PM