快捷键:

Power +volume down/ volume up键
Android 4.0 以上支持该功能, 需 同时按住Power 键和 volume down/ volume up键, 即可截取当前手机屏幕保存至sd卡中

命令:
adb shell screencap -p filepath
 
     
  1. screencap -h

  2. usage: screencap [-hp] [FILENAME]

  3.   -h: this message

  4.   -p: save the file as a png.

  5. If FILENAME ends with .png it will be saved as a png.

  6. If FILENAME isnot given, the results will be printed to stdout.


代码实现:

1.通过读取bitmap来实现截屏,此种方法只适用于对本应用自身的Activity 进行截屏。


 
   
  1. publicvoid takeScreenShot(View view ){

  2.     view.setDrawingCacheEnabled( true);

  3.     view.buildDrawingCache();

  4.     Bitmap bitmap = view.getDrawingCache();

  5. if (bitmap != null) {

  6. try {

  7.         FileOutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory()+File. separator "screenshot.png" );

  8.         bitmap.compress(Bitmap.CompressFormat. PNG, 100, out);

  9.       } catch (Exception e) {

  10.         e.printStackTrace();

  11.       }

  12.     }

  13. }


2.在APK中调用“ adb shell screencap -p filepath” 命令
该命令读取系统的framebuffer,需要获得系统权限:
(1). 在AndroidManifest.xml文件中添加
< uses-permission android:name = "android.permission.READ_FRAME_BUFFER" />
(2). 修改APK为系统权限,将APK放到源码中编译, 修改Android.mk
 LOCAL_CERTIFICATE := platform

 
  
  1. publicvoid takeScreenShot(){

  2.    String mSavedPath = Environment.getExternalStorageDirectory()+File. separator + "screenshot.png" ;

  3. try {                    

  4.           Runtime. getRuntime().exec("screencap -p " + mSavedPath);

  5.    } catch (Exception e) {

  6.           e.printStackTrace();

  7.    }


3.利用系统的API,实现Screenshot,这部分代码是系统隐藏的,需要在源码下编译,

    1).修改Android.mk, 添加系统权限
          LOCAL_CERTIFICATE := platform
         2).修改AndroidManifest.xml 文件,添加
权限
< uses-permission android:name = "android.permission.READ_FRAME_BUFFER" />
      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 ;
}