android webView截图 android webView截图的4种方法

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

android webView截图 android webView截图的4种方法

PalmerYang   2021-03-28 我要评论
想了解android webView截图的4种方法的相关内容吗,PalmerYang在本文为您仔细讲解android webView截图的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,webView,截图,下面大家一起来学习吧。

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

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们