user6048358
user6048358

Reputation: 1

How to get WebView screenshot

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

Upvotes: 0

Views: 2288

Answers (1)

Stanley Ko
Stanley Ko

Reputation: 3497

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

Upvotes: 1

Related Questions