WebView屏幕截图捕获整个页面

时间:2016-06-15 08:02:09

标签: android webview

我已经尝试了stackoverflow上可用的所有解决方案来解决这个问题,但我仍然无法捕获整个webview页面。我只是在快照中获取页面的可见区域.MeasureSpec在这里不起作用。这里是我的代码: -

public void startPlugin() {
 browser.setWebViewClient(new MyBrowser());
 browser.getSettings().setLoadsImagesAutomatically(true);
 browser.getSettings().setJavaScriptEnabled(true);
 browser.setScrollbarFadingEnabled(true);
 browser.setHorizontalScrollBarEnabled(false);
 browser.loadUrl("http://192.168.1.11:8080");
 setTitle("Plugin");

}

public void takeScreenShotAndSend() {
    View view = browser;
    view.setDrawingCacheEnabled(true);


    File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES), "abcd");

    Picture picture = browser.capturePicture();
    PictureDrawable pd = new PictureDrawable(picture);
    Bitmap myBitmap = Bitmap.createBitmap(pd.getIntrinsicWidth(),
            pd.getIntrinsicHeight(), Bitmap.Config.ARGB_8888);
    Canvas c = new Canvas(myBitmap);

    c.drawPicture(pd.getPicture());

    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.e("Eckovation", "failed to create directory");
            return;
        }
    }

    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    String imagePath = mediaStorageDir.getPath() + File.separator +
            "IMG_" + timeStamp + ".jpg";
    ;

    File mediaFile;

    mediaFile = new File(imagePath);

    try {
        FileOutputStream fos = new FileOutputStream(mediaFile);
        myBitmap.compress(Bitmap.CompressFormat.JPEG, getResources().getInteger(R.integer.jpeg_quality), fos);
        fos.flush();
        fos.close();
        shareImage(imagePath);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    view.destroyDrawingCache();
}

0 个答案:

没有答案