android webView截图的4种方法
作者:PalmerYang 时间:2023-02-04 19:54:12
android 在webView里面截图大概有四种方式,具体内容如下
1.获取到DecorView然后将DecorView转换成bitmap然后写入到文件里面.
View view = getWindow().getDecorView();
Bitmap bitmap = Bitmap.createBitmap(view.getWidth(), view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
Log.d(TAG,"bitmap--"+bitmap);
try {
String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_jietu.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
//压缩bitmap到输出流中
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromGetDecorView.this, "截屏成功", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}finally {
if(bitmap!=null) {
bitmap.recycle();
}
}
2.使用webViewpicture来实现该功能.(该方法被废弃了因此不建议使用)
Picture picture = webView.capturePicture();
int width = picture.getWidth();
int height = picture.getHeight();
if (width > 0 && height > 0) {
Bitmap bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
picture.draw(canvas);
try {
String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_jietu.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
//压缩bitmap到输出流中
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromCapture.this, "截屏成功", Toast.LENGTH_LONG).show();
bitmap.recycle();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
}
3.使用webViewDraw来实现.(该方法被废弃了因此不建议使用)
float scale = webView.getScale();
int webViewHeight = (int) (webView.getContentHeight()*scale+0.5);
Bitmap bitmap = Bitmap.createBitmap(webView.getWidth(),webViewHeight, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webView.draw(canvas);
try {
String fileName = Environment.getExternalStorageDirectory().getPath()+"/webview_jietu.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
//压缩bitmap到输出流中
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
fos.close();
Toast.makeText(WebviewFromDraw.this, "截屏成功", Toast.LENGTH_LONG).show();
bitmap.recycle();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
4.使用webViewDrawCache来实现(建议使用).
Bitmap bitmap = webView.getDrawingCache();
try {
String fileName = Environment.getExternalStorageDirectory().getPath() + "/webview_jietu.jpg";
FileOutputStream fos = new FileOutputStream(fileName);
//压缩bitmap到输出流中
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, fos);
bitmap.recycle();
fos.close();
Toast.makeText(WebviewFromDrawCache.this, "截屏成功", Toast.LENGTH_LONG).show();
} catch (Exception e) {
Log.e(TAG, e.getMessage());
} finally {
bitmap.recycle();
}
注意:
在android5.0及以上版本使用webView进行截长图时,默认是截取可是区域内的内容.因此需要在支撑窗体内容之前加上如下方法.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
WebView.enableSlowWholeDocumentDraw();
}
setContentView(R.layout.activity_webview);
来源:https://blog.csdn.net/unreliable_narrator/article/details/75650363
标签:android,webView,截图
0
投稿
猜你喜欢
Java 事务注解@Transactional回滚(try catch、嵌套)问题
2021-05-29 17:54:43
java文件操作之java写文件简单示例
2023-10-21 14:37:13
JDK1.8下载、安装和环境配置超详细教程(最新最完整)
2022-07-22 12:58:34
Java RabbitMQ高级特性详细分析
2021-12-26 00:31:36
SpringBoot+LayIM+t-io 实现好友申请通知流程
2023-07-13 11:41:52
java实现将结果集封装到List中的方法
2021-10-27 22:29:45
详解Java对象结构与对象锁的升级
2021-12-05 16:18:38
Unity实现仿3D轮转图效果
2023-11-24 12:26:56
Android实现屏幕保持常亮功能
2023-08-09 13:20:15
Java对象类型的判断详解
2023-07-26 09:55:07
Android 8.0实现蓝牙遥控器自动配对
2021-08-05 08:24:16
Java中的匿名内部类小结
2021-05-29 06:29:38
Java Comparable和Comparator对比详解
2022-08-13 01:28:08
Java(基于Struts2) 分页实现代码
2023-11-04 05:58:58
mvc开启gzip压缩示例分享
2022-05-03 08:34:29
C# DES加密算法中向量的作用详细解析
2022-07-13 07:49:48
Java等待唤醒机制线程通信原理解析
2022-03-31 00:37:21
Spring Boot + Thymeleaf + Activiti 快速开发平台项目 附源码
2023-11-23 08:23:43
MyBatis多表关联查询的实现示例
2023-07-06 06:26:49
Android Studio 2022.1.1创建项目的Gradle配置问题
2022-05-05 21:45:36