命令:
screencap -h
usage: screencap [-hp] [FILENAME]
-h: this message
-p: save the file as a png.
If FILENAME ends with .png it will be saved as a png.
If FILENAME isnot given, the results will be printed to stdout.
1.通过读取bitmap来实现截屏,此种方法只适用于对本应用自身的Activity 进行截屏。
publicvoid takeScreenShot(View view ){
view.setDrawingCacheEnabled( true);
view.buildDrawingCache();
Bitmap bitmap = view.getDrawingCache();
if (bitmap != null) {
try {
FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory()+File. separator "screenshot.png" );
bitmap.compress(Bitmap.CompressFormat. PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
}
}
}
(1). 在AndroidManifest.xml文件中添加<uses-permissionandroid:name="android.permission.READ_FRAME_BUFFER"/>(2). 修改APK为系统权限,将APK放到源码中编译, 修改Android.mkLOCAL_CERTIFICATE := platform
publicvoid takeScreenShot(){
String mSavedPath = Environment.getExternalStorageDirectory()+File. separator + "screenshot.png" ;
try {
Runtime. getRuntime().exec("screencap -p " + mSavedPath);
} catch (Exception e) {
e.printStackTrace();
}
3.利用系统的API,实现Screenshot,这部分代码是系统隐藏的,需要在源码下编译,
public boolean takeScreenShot(String p_w_picpathPath){ if(p_w_picpathPath.equals("" )){ p_w_picpathPath = Environment.getExternalStorageDirectory()+File. separator+"Screenshot.png" ; } Bitmap mScreenBitmap; WindowManager mWindowManager; DisplayMetrics mDisplayMetrics; Display mDisplay; mWindowManager = (WindowManager) mcontext.getSystemService(Context.WINDOW_SERVICE); mDisplay = mWindowManager.getDefaultDisplay(); mDisplayMetrics = new DisplayMetrics(); mDisplay.getRealMetrics(mDisplayMetrics); float[] dims = {mDisplayMetrics.widthPixels , mDisplayMetrics.heightPixels }; mScreenBitmap = Surface. screenshot((int) dims[0], ( int) dims[1]); if (mScreenBitmap == null) { return false ; } try { FileOutputStream out = new FileOutputStream(p_w_picpathPath); mScreenBitmap.compress(Bitmap.CompressFormat. PNG, 100, out); } catch (Exception e) { return false ; } return true ; }