如何在Android中捕获完整(滚动)的Webview(恰好。我捕获了,但另一面是白色的)

时间:2019-01-24 10:07:30

标签: android webview capture

我想要所有的webview捕获...但这并不容易...

我使用基本的Webview。

我的问题是仅捕获屏幕空间。我需要所有webview的图片 (我的结果在图片下方)

我尝试了很多方法。

但这不起作用

在下面查看我的代码

//关于Webview设置的MainActivity。

    wvMain = findViewById(R.id.wvMain);
    WebSettings mWebSettings = wvMain.getSettings();
    mWebSettings.setJavaScriptEnabled(true);
    mWebSettings.setLoadWithOverviewMode(true);
    mWebSettings.setUseWideViewPort(true);
    mWebSettings.setSupportZoom(true);
    mWebSettings.setBuiltInZoomControls(true);
    mWebSettings.setDisplayZoomControls(false);
    wvMain.setNestedScrollingEnabled(true);
    wvMain.setVerticalScrollBarEnabled(true);

    wvMain.setWebViewClient(new CaptureWebViewClient());

    wvMain.loadUrl("https://www.youtube.com/");

//捕获通话部分

class Capture implements Runnable {

    @Override
    public void run() {
        Integer[] integers = new Integer[]{100};
        new AsyncCapture(context, wvMain).execute(integers);
    }
}

// AsyncCapture类

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.AsyncTask;
import android.text.format.DateFormat;
import android.view.View;
import android.webkit.WebView;
import android.widget.Toast;

import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;

public class AsyncCapture extends AsyncTask<Integer, Integer, String[]> {
    private Bitmap mBitmap;
    private Context mContext;
private WebView mWebView;

public AsyncCapture(Context mContext, WebView mWebView) {
    this.mContext = mContext;
    this.mWebView = mWebView;
}

@Override
protected void onPreExecute() {
    try {
        int density = (int) this.mContext.getResources().getDisplayMetrics().density;
        mWebView.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
                View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));

        this.mBitmap = Bitmap.createBitmap(
                this.mWebView.getMeasuredWidth(), this.mWebView.getMeasuredHeight(), Bitmap.Config.RGB_565);
        this.mWebView.draw(new Canvas(this.mBitmap));

        Toast.makeText(mContext, "PreExcute Done", Toast.LENGTH_SHORT).show();
    } catch (OutOfMemoryError oome) {
        Toast.makeText(mContext, "OOM ERROR", Toast.LENGTH_SHORT).show();
        cancel(true);
    } catch (Exception e) {
        e.printStackTrace();
        cancel(true);
    }
    super.onPreExecute();
}

@Override
protected String[] doInBackground(Integer... integers) {
    try {
        String path = FileUtil.getAppDirectoryPath(this.mContext);
        String filepath = path + "/" + (DateFormat.format("yyyyMMdd-HHmmss", new Date()) + ".png");
        if (!new File(path).exists()) {
            new File(path).mkdirs();
        }
        FileOutputStream mFileOutputStream = new FileOutputStream(new File(filepath));
        this.mBitmap.compress(Bitmap.CompressFormat.PNG, 100, mFileOutputStream);
        mFileOutputStream.flush();
        mFileOutputStream.close();
        Toast.makeText(mContext, "doInBackground Done", Toast.LENGTH_SHORT).show();
        return new String[]{"ok", filepath};
    } catch (Exception e) {
        e.printStackTrace();
        return new String[]{"error", e.toString()};
    }
}

@Override
protected void onPostExecute(String[] result) {
    if (this.mBitmap != null) {
        this.mBitmap.recycle();
        this.mBitmap = null;
    }
    Intent mIntent = new Intent("good");
    mIntent.putExtra("result", result);
    this.mContext.sendBroadcast(mIntent);
    Toast.makeText(mContext, "onPostExecute Done", Toast.LENGTH_SHORT).show();

    super.onPostExecute(result);
}

protected void onCancelled() {
    super.onCancelled();
    if (this.mBitmap != null) {
        this.mBitmap.recycle();
        this.mBitmap = null;
    }
    Intent mIntent = new Intent("good");
    this.mContext.sendBroadcast(mIntent);
}

}

///////////////////////////////////////////////// ///////////// 不是结果

enter image description here

可以很好地捕捉...但是只是屏幕尺寸!

请帮助我。谢谢!

0 个答案:

没有答案