How to get WebView screenshot

£可爱£侵袭症+ 提交于 2020-01-17 14:56:16

问题


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

these codes not used in xiaomi


回答1:


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

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


来源:https://stackoverflow.com/questions/35933030/how-to-get-webview-screenshot

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!