首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何以编程方式截取the视图的屏幕截图,从而捕获整个页面?

如何以编程方式截取the视图的屏幕截图,从而捕获整个页面?
EN

Stack Overflow用户
提问于 2012-03-17 07:58:24
回答 3查看 29.9K关注 0票数 30

我认为标题几乎涵盖了它,但我在我的活动中有一个webview。我已经在网页视图中加载了一个url,我想要截取整个页面的屏幕截图(无论是视口中的内容还是“折叠下”的内容)。

我已经有了截图视口的代码,我知道这可以在iOS中通过在截图之前放大webview来完成。我在这里尝试使用了相同的技术:

代码语言:javascript
复制
    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);
    }

但我仍然只捕捉到了视口。抛开页面太大导致内存不足之类的事情不谈,有没有人知道我做错了什么,或者我如何才能包括视区之外的部分?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2013-01-07 19:38:19

试试这个

代码语言:javascript
复制
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文件中添加上网权限和WRITE_EXTERNAL_STORAGE。

如果应用程序在棉花糖上或以上运行,则需要在运行时询问文件写入权限。

票数 26
EN

Stack Overflow用户

发布于 2019-05-15 10:28:07

webview.capturePicture()已弃用,使用下面的方法可以做到:

代码语言:javascript
复制
Bitmap bitmap = Bitmap.createBitmap(webview.getWidth(), webview.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
webview.draw(canvas);
return bitmap;
票数 7
EN

Stack Overflow用户

发布于 2015-03-14 17:48:49

@avinash thakur代码实际上是有效的,你只是忘记在Android Manifest文件中添加<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>,否则,android不会允许你的流在设备上写入数据/文件。

干杯。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9745988

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档