android webview capturepicture,android webView截图的4种方法

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);

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值