通过一下方法无法截取到控件中带视频播放器的内容 截屏出来是白屏状态
// 开始截屏 private static byte[] screenshotView() { View view = getWindow().getDecorView(); // view.setDrawingCacheEnabled(true); // 设置缓存,可用于实时截图 Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); view.draw(canvas); // view.setDrawingCacheEnabled(false); // 清空缓存,可用于实时截图 byte[] drawByte = getBitmapByte(bitmap); // 位图转为 Byte return drawByte; }
// 位图转 Base64 String private static String getBitmapString(Bitmap bitmap) { String result = null; ByteArrayOutputStream out = null; try { if (bitmap != null) { out = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();
byte[] bitmapBytes = out.toByteArray();
result = Base64.encodeToString(bitmapBytes, Base64.DEFAULT);
}
} catch (IOException e) { e.printStackTrace(); } finally { try { if (out != null) { out.flush(); out.close(); } } catch (IOException e) { e.printStackTrace(); } } return result; }
// 位图转 Byte private static byte[] getBitmapByte(Bitmap bitmap){ ByteArrayOutputStream out = new ByteArrayOutputStream(); // 参数1转换类型,参数2压缩质量,参数3字节流资源 bitmap.compress(Bitmap.CompressFormat.PNG, 100, out); try { out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } return out.toByteArray(); }
需要截屏到视频画面 就需要使用系统截屏方法MediaProjection()