2015-10-16 85 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