如何以编程方式截取 webview 的屏幕截图,捕获整个页面? [英] How can I programmatically take a screenshot of a webview, capturing the full page?

查看:21
本文介绍了如何以编程方式截取 webview 的屏幕截图,捕获整个页面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我认为标题几乎涵盖了它,但我的活动中有一个网络视图.我已经将一个 url 加载到 webview 中,我想截取整个页面的屏幕截图(视口中的任何内容以及折叠下方"的内容).

I think the title pretty much covers it, but I have a webview in my activity. I've loaded a url into the webview and I'd like to take a screenshot of the full page (whatever is in the viewport and the stuff "below the fold" as well).

我有可以对视口进行快照的代码,我知道这可以在 iOS 中通过在拍摄之前放大 webview 来完成.我在这里尝试使用相同的技术:

I've got code that works to snapshot the viewport, and I know this can be done in iOS by enlarging the webview before snapshotting it. I've tried to use the same technique here:

    WebView browserView = (WebView) findViewById(R.id.browserView);

    //Resize the webview to the height of the webpage
    int pageHeight = browserView.getContentHeight();
    LayoutParams browserParams = browserView.getLayoutParams();
    browserView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, pageHeight));

    //Capture the webview as a bitmap
    browserView.setDrawingCacheEnabled(true);
    Bitmap bitmap = Bitmap.createBitmap(browserView.getDrawingCache());
    browserView.setDrawingCacheEnabled(false);

    //Create the filename to use
    String randomFilenamepart = String.valueOf(new SecureRandom().nextInt(1000000));
    String filename = Environment.getExternalStorageDirectory().toString() + "/Screenshot_" + randomFilenamepart + ".jpg";
    File imageFile = new File(filename);
    //Stream the file out to external storage as a JPEG
    OutputStream fout = null;
    try {
        fout = new FileOutputStream(imageFile);
        bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
        fout.flush();
        fout.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        browserView.setLayoutParams(browserParams);
    }

但我仍然只捕获视口.不考虑由于页面太大而导致内存不足等问题,有谁知道我做错了什么,或者我如何将视口外的部分包含在内?

But I'm still only capturing just the viewport. Disregarding things like running out of memory because the page is too large, does anyone know what I'm doing wrong or how I can include the portion outside the viewport?

推荐答案

试试这个

import java.io.FileOutputStream;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.os.Bundle;
import android.view.Menu;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {

    WebView w;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        w = new WebView(this);
        w.setWebViewClient(new WebViewClient() {
            public void onPageFinished(WebView view, String url) {
                Picture picture = view.capturePicture();
                Bitmap b = Bitmap.createBitmap(picture.getWidth(),
                        picture.getHeight(), Bitmap.Config.ARGB_8888);
                Canvas c = new Canvas(b);

                picture.draw(c);
                FileOutputStream fos = null;
                try {

                    fos = new FileOutputStream("mnt/sdcard/yahoo.jpg");
                    if (fos != null) {
                        b.compress(Bitmap.CompressFormat.JPEG, 100, fos);

                        fos.close();
                    }
                } catch (Exception e) {

                }
            }
        });

        setContentView(w);
        w.loadUrl("http://search.yahoo.com/search?p=android");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

在 AndroidManifest.xml 文件中添加 INTERNET PERMISSION 和 WRITE_EXTERNAL_STORAGE.

Add INTERNET PERMISSION and WRITE_EXTERNAL_STORAGE in AndroidManifest.xml file.

如果应用在 Marshmallow 上运行,则需要在运行时请求许可才能写入文件.

Need to ask permission at run time for file write if app is running on or above Marshmallow.

这篇关于如何以编程方式截取 webview 的屏幕截图,捕获整个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆