项目需求中需要把webview的网页内容导出为图片和PDF,方法如下

导出为PDF

注意: 该方法只能在API 19+可以使用

代码如下:

 public void printPDF() {        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {            // Get a PrintManager instance            PrintManager printManager = (PrintManager) getSystemService(Context.PRINT_SERVICE);            // Get a print adapter instance            PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();            // Create a print job with name and adapter instance            String jobName = getString(R.string.app_name) + " Document";            printManager.print(jobName, printAdapter,                    new PrintAttributes.Builder().build());        } else {            Toast.makeText(getApplicationContext(), "当前系统不支持该功能", Toast.LENGTH_SHORT).show();        }    }

导出为图片

代码如下:

需要权限:    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>    /** * 保存图片 * * @param view */    public void saveImage(View view) {        Picture picture = webView.capturePicture();        Bitmap b = Bitmap.createBitmap(                picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);        Canvas c = new Canvas(b);        picture.draw(c);        File file = new File("/sdcard/" + "page.jpg");        if(file.exists()){            file.delete();        }        FileOutputStream fos = null;        try {            fos = new FileOutputStream(file.getAbsoluteFile());            if (fos != null) {                b.compress(Bitmap.CompressFormat.JPEG, 90, fos);                fos.close();                Toast.makeText(getApplicationContext(), "保存成功", Toast.LENGTH_SHORT).show();            }        } catch (Exception e) {            e.printStackTrace();            Toast.makeText(getApplicationContext(), "保存失败", Toast.LENGTH_SHORT).show();        }    }

我的github: https://github.com/a1018875550,欢迎follow;

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. Android_UI_全屏去掉标题栏和状态栏的两种方法
  3. Android(安卓)自带的系统分享功能
  4. Android程序监听Home键
  5. Android(安卓)代码修改按钮上的图片
  6. Android(安卓)Studio 实现九宫格
  7. Android(安卓)MaterialSheetFab
  8. Android启动页图片适配
  9. Android(安卓)上传文件,头像。流形式

随机推荐

  1. 某汽车零部件企业研发软件许可监控与优化
  2. Golang笔记之流程控制
  3. MongoDB实战篇:高级查询----$elemMatch与a
  4. Veeam v11 重量级功能 CDP 持续数据保护(
  5. 培训学web前端前景如何?
  6. 索刻科技CRM用户手册
  7. html基础:table入门
  8. html基础:form表单
  9. php第一课vscode安装
  10. 第二课 课程表与注册表单