如何获取WebView截图

时间:2016-03-11 05:39:09

标签: android webview

如何获取整页WebView截图?

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

这些代码未在xiaomi中使用

1 个答案:

答案 0 :(得分:1)

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

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

  

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