2016-01-25 43 views
-2

在Android上我可以通過活動管理器執行getRunningAppProcesses運行的應用程序的列表:獲取其他正在運行的應用程序的截圖在Android

 ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE); 
    List<ActivityManager.RunningAppProcessInfo> runningAppProcessInfo = am.getRunningAppProcesses(); 

但我想更多的東西,當我打開系統任務管理器,有截圖正在運行的應用程序,如 Screenshot 我可以在我自己的應用程序中獲得這些屏幕截圖嗎?如何獲得表示給定窗口內容的圖像?

Endriu

+0

你需要看到你的應用程序內部的截圖?或者您只想在設備上製作其他應用的屏幕截圖? –

+0

我想在我的應用程序中看到它們。 –

回答

0

截屏無需要root權限:

@SuppressLint("NewApi") 
    public void takeScreenshot(Context context, String fileFullPath) 
    { 
     if(fileFullPath == ""){ 
      format = new SimpleDateFormat("yyyyMMddHHmmss"); 
      String fileName = format.format(new Date(System.currentTimeMillis())) + ".png"; 
      fileFullPath = "/data/local/tmp/" + fileName; 
     } 

     if(ShellUtils.checkRootPermission()){ 
      if(android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){ 
       ShellUtils.execCommand("/system/bin/screencap -p "+ fileFullPath,true); 
      } 
     } 
     else { 
      if(android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2 && android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH){ 
       wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE); 
       mDisplay = wm.getDefaultDisplay(); 
       mDisplayMatrix = new Matrix(); 
       mDisplayMetrics = new DisplayMetrics(); 
       // We need to orient the screenshot correctly (and the Surface api seems to take screenshots 
       // only in the natural orientation of the device :!) 
       mDisplay.getRealMetrics(mDisplayMetrics); 
       float[] dims = 
       { 
         mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels 
       }; 
       float degrees = getDegreesForRotation(mDisplay.getRotation()); 
       boolean requiresRotation = (degrees > 0); 
       if (requiresRotation){ 
        // Get the dimensions of the device in its native orientation 
        mDisplayMatrix.reset(); 
        mDisplayMatrix.preRotate(-degrees); 
        mDisplayMatrix.mapPoints(dims); 
        dims[0] = Math.abs(dims[0]); 
        dims[1] = Math.abs(dims[1]); 
       } 

       Bitmap mScreenBitmap = screenShot((int) dims[0], (int) dims[1]); 
       if (requiresRotation) { 
        // Rotate the screenshot to the current orientation 
        Bitmap ss = Bitmap.createBitmap(mDisplayMetrics.widthPixels, mDisplayMetrics.heightPixels, 
          Bitmap.Config.ARGB_8888); 
        Canvas c = new Canvas(ss); 
        c.translate(ss.getWidth()/2, ss.getHeight()/2); 
        c.rotate(degrees); 
        c.translate(-dims[0]/2, -dims[1]/2); 
        c.drawBitmap(mScreenBitmap, 0, 0, null); 
        c.setBitmap(null); 
        mScreenBitmap = ss; 
        if (ss != null && !ss.isRecycled()) { 
         ss.recycle(); 
        } 
       } 

       // If we couldn't take the screenshot, notify the user 
       if (mScreenBitmap == null){ 
        Toast.makeText(context, "screen shot fail", Toast.LENGTH_SHORT).show(); 
       } 

       // Optimizations 
       mScreenBitmap.setHasAlpha(false); 
       mScreenBitmap.prepareToDraw(); 

       saveBitmap2file(context, mScreenBitmap, fileFullPath); // save image file 
      } 
     } 

    } 

來源:ScreentShotUtil.java

+0

這不完全是我的意思。我不想截圖當前屏幕,我有最小化應用程序的屏幕截圖/預覽,完全與附加圖像一樣(內置任務管理器以某種方式可以獲取它們)。 –

相關問題