2015-10-16 81 views
0

我想在沒有截圖的情況下捕獲位圖中的android屏幕。 請幫助我。如何獲取Android屏幕的位圖?

謝謝。

+0

退房:HTTP:// stackoverflow.com/questions/2661536/how-to-programmatically-take-a-screenshot-in-android –

+0

我想獲取位圖而不需要屏幕截圖。請幫助我如何做到這一點。 – Gazal

+0

'我想獲取位圖而不需要屏幕截圖。「你知道**這是廢話嗎? –

回答

4

試試這個:

private void takeScreenshot() { 
    Date now = new Date(); 
    android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now); 

    try { 
     // image naming and path to include sd card appending name you choose for file 
     String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg"; 

     // create bitmap screen capture 
     View v1 = getWindow().getDecorView().getRootView(); 
     v1.setDrawingCacheEnabled(true); 
     Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache()); 
     v1.setDrawingCacheEnabled(false); 

     File imageFile = new File(mPath); 

     FileOutputStream outputStream = new FileOutputStream(imageFile); 
     int quality = 100; 
     bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream); 
     outputStream.flush(); 
     outputStream.close(); 

     openScreenshot(imageFile); 
    } catch (Throwable e) { 
     // Several error may come out with file handling or OOM 
     e.printStackTrace(); 
    } 
} 

您可以查看這樣的文件:

private void openScreenshot(File imageFile) { 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_VIEW); 
    Uri uri = Uri.fromFile(imageFile); 
    intent.setDataAndType(uri, "image/*"); 
    startActivity(intent); 
} 

添加此權限保存文件:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
+0

我想獲得的位圖不是特定的應用程序,而是顯示給用戶的任何屏幕。 其次我想知道我們是否需要開發一個應用程序來獲取任何android屏幕的位圖,或者我們可以通過jar文件來做到這一點。 – Gazal

+0

@Gazal'我想得到的不是特定應用程序的位圖,而是任何顯示給用戶的屏幕。簡言之:**你不允許**。 –

+0

我們可以將位圖僅用於不適用於android屏幕的圖像嗎? 如果是這樣,那麼我們可以直接從屏幕上取像素值而不創建位圖嗎? – Gazal