如何获取 WebView 屏幕截图 [英] How to get WebView screenshot

查看:52
本文介绍了如何获取 WebView 屏幕截图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何获取完整页面的 WebView 截图?

how to get the full page WebView Screenshot?

snapShot= webView.capturePicture();
capture = Bitmap.createBitmap(snapShot.getWidth(),snapShot.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(capture);
snapShot.draw(canvas);

小米未使用这些代码

推荐答案

您可以使用这段代码,其中 mPath 是保存文件的路径, mMainView 是要捕获的视图.

You can use this code, where mPath is path to save file, and mMainView is view to capture.

如果要更改文件类型或压缩率,请修改以下代码.

If you want to change file type or compress rate, modify below code.

bitmap.compress(Bitmap.CompressFormat.PNG, 90, fout);

bitmap.compress(Bitmap.CompressFormat.PNG, 90, fout);

private void captureScreen() {

    String mPath = getAppStorageFolder(getActivity()) + File.separator + "test.png";

    // create bitmap screen capture
    Bitmap bitmap;
    View v1 = webView.getRootView();
    v1.setDrawingCacheEnabled(true);
    bitmap = Bitmap.createBitmap(v1.getDrawingCache());
    v1.setDrawingCacheEnabled(false);

    OutputStream fout = null;
    File imageFile = new File(mPath);

    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.PNG, 90, fout);
        fout.flush();
        fout.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

public String getAppStorageFolder(Activity activity) {
    return Environment.getExternalStorageDirectory() + File.separator + activity.getString(R.string.app_name);
}

这篇关于如何获取 WebView 屏幕截图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆