android - 从 Android 中的 WebView 截取屏幕截图

标签 android webview

我有 html 源代码,并且使用 WebView 加载此源代码:

WebView webView = (WebView) new WebView(getActivity()); 
        webView = (WebView) rootView.findViewById(R.id.webView1);

        webView.getSettings().setJavaScriptEnabled(true);
        String sourceHtml = (String) this.getActivity().getIntent().getExtras().get(ROW_ID1);


        webView.loadData(sourceHtml, "text/html", "UTF-8");

现在我想将此 WebView 保存在 BitMap 中或 SdCard 中的其他内容,我遵循了一些指南但没有成功,我该怎么做?

最佳答案

尝试使用capturePicture函数:

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/screenshot.jpg" );
        if ( fos != null )
        {
            b.compress(Bitmap.CompressFormat.JPEG, 100, fos);
            fos.close();
        }
    }
catch(Exception e) {}

关于android - 从 Android 中的 WebView 截取屏幕截图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20919431/

相关文章:

android - Grid Recyclerview,偶数行有 3 列,奇数行有 4 列

android - 在图像上写文字的最佳方式?

Android从gmail应用程序获取附加文件名

Android 的 WebView 中的 JavascriptInterface : multiple calls to JS cause deadlock

macos - 为什么 Safari 和 WebView 组件之间的插件集可能不同?

android - Paint.setAlpha() 未按预期工作

android - LayoutInflater 似乎没有与布局中的 id 链接

javascript - 如何获取 Android WebView 中从顶部到 div block 的像素数量?

android - 如何在 webview 中设置固定缩放级别或缩小?

android - 停止对讲,加载后说 Webview