http://stackoverflow.com/questions/10296711/androidtake-screenshot-and-share-it/10296881#10296881

 

  1. private static Bitmap takeScreenShot(Activity activity) { 
  2.         View view = activity.getWindow().getDecorView(); 
  3.         view.setDrawingCacheEnabled(true); 
  4.         view.buildDrawingCache(); 
  5.         Bitmap b1 = view.getDrawingCache(); 
  6.         Rect frame = new Rect(); 
  7.         activity.getWindow().getDecorView().getWindowVisibleDisplayFrame(frame); 
  8.         int statusBarHeight = frame.top; 
  9.         int width = activity.getWindowManager().getDefaultDisplay().getWidth(); 
  10.         int height = activity.getWindowManager().getDefaultDisplay() 
  11.                 .getHeight(); 
  12.         // Bitmap b = Bitmap.createBitmap(b1, 0, 25, 320, 455); 
  13.         Bitmap b = Bitmap.createBitmap(b1, 0, statusBarHeight, width, height 
  14.                 - statusBarHeight); 
  15.         view.destroyDrawingCache(); 
  16.         return b; 
  17.     } 
  18.  
  19.     private static void savePic(Bitmap b, String strFileName) { 
  20.         FileOutputStream fos = null
  21.         try { 
  22.             fos = new FileOutputStream(strFileName); 
  23.             if (null != fos) { 
  24.                 b.compress(Bitmap.CompressFormat.PNG, 90, fos); 
  25.                 fos.flush(); 
  26.                 fos.close(); 
  27.             } 
  28.         } catch (FileNotFoundException e) { 
  29.             e.printStackTrace(); 
  30.         } catch (IOException e) { 
  31.             e.printStackTrace(); 
  32.         } 
  33.     } 
  34.  
  35.     public static void shoot(Activity a, String b) { 
  36.         // savePic(takeScreenShot(a), "sdcard/xx.png"); 
  37.         savePic(takeScreenShot(a), b); 
  38.     }