如何捕获android设备屏幕内容?

如何捕获android设备屏幕内容?

如何使用快照数据捕获android设备屏幕内容并制作图像文件?我应该使用哪种API或在哪里可以找到相关资源?

BTW:不是相机快照,而是设备屏幕


长风秋雁
浏览 1306回答 3
3回答

宝慕林4294392

根据这个链接,可以在android sdk的tools目录中使用ddms进行屏幕截图。要在应用程序中执行此操作(而不是在开发期间),还有一些应用程序可以执行此操作。但正如@ zed_0xff指出的那样,肯定需要root。

ITMISS

使用以下代码:Bitmap bitmap;View v1 = MyView.getRootView();v1.setDrawingCacheEnabled(true);bitmap = Bitmap.createBitmap(v1.getDrawingCache());v1.setDrawingCacheEnabled(false);这MyView是View我们需要在屏幕中包含的内容。你也可以DrawingCache通过View这种方式获得(没有getRootView())。还有另一种方式.. 如果我们ScrollView以root身份查看,那么最好使用以下代码,LayoutInflater inflater = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);FrameLayout root = (FrameLayout) inflater.inflate(R.layout.activity_main, null); // activity_main is UI(xml) file we used in our Activity class. FrameLayout is root view of my UI(xml) file.root.setDrawingCacheEnabled(true);Bitmap bitmap = getBitmapFromView(this.getWindow().findViewById(R.id.frameLayout)); // here give id of our root layout (here its my FrameLayout's id)root.setDrawingCacheEnabled(false);这是getBitmapFromView()方法public static Bitmap getBitmapFromView(View view) {         //Define a bitmap with the same size as the view         Bitmap returnedBitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(),Bitmap.Config.ARGB_8888);         //Bind a canvas to it         Canvas canvas = new Canvas(returnedBitmap);         //Get the view's background         Drawable bgDrawable =view.getBackground();         if (bgDrawable!=null)              //has background drawable, then draw it on the canvas             bgDrawable.draw(canvas);         else              //does not have background drawable, then draw white background on the canvas             canvas.drawColor(Color.WHITE);         // draw the view on the canvas         view.draw(canvas);         //return the bitmap         return returnedBitmap;     }它将显示整个屏幕,包括隐藏在ScrollView 还有另一种更好的截屏方式。我在这里截了屏幕截图WebView。WebView w = new WebView(this);     w.setWebViewClient(new WebViewClient()     {         public void onPageFinished(final WebView webView, String url) {             new Handler().postDelayed(new Runnable(){                 @Override                 public void run() {                     webView.measure(View.MeasureSpec.makeMeasureSpec(                                     View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED),                             View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));                     webView.layout(0, 0, webView.getMeasuredWidth(),                             webView.getMeasuredHeight());                     webView.setDrawingCacheEnabled(true);                     webView.buildDrawingCache();                     Bitmap bitmap = Bitmap.createBitmap(webView.getMeasuredWidth(),                             webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);                     Canvas canvas = new Canvas(bitmap);                     Paint paint = new Paint();                     int height = bitmap.getHeight();                     canvas.drawBitmap(bitmap, 0, height, paint);                     webView.draw(canvas);                     if (bitmap != null) {                         try {                             String filePath = Environment.getExternalStorageDirectory()                                     .toString();                             OutputStream out = null;                             File file = new File(filePath, "/webviewScreenShot.png");                             out = new FileOutputStream(file);                             bitmap.compress(Bitmap.CompressFormat.PNG, 50, out);                             out.flush();                             out.close();                             bitmap.recycle();                         } catch (Exception e) {                             e.printStackTrace();                         }                     }                 }             }, 1000);         }     });希望这可以帮助..!

ibeautiful

AFAIK,目前捕获android截图的所有方法都使用/ dev / graphics / fb0 framebuffer。这包括ddms。它确实需要root才能从此流中读取。ddms使用adbd来请求信息,因此不需要root,因为adb具有从/ dev / graphics / fb0请求数据所需的权限。帧缓冲包含2 +“帧”的RGB565图像。如果您能够读取数据,则必须知道屏幕分辨率才能知道获取图像需要多少字节。每个像素为2个字节,因此如果屏幕分辨率为480x800,则必须为该图像读取768,000个字节,因为480x800 RGB565图像具有384,000个像素。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android