以截图的的WebView Android中截图、WebView、Android

2023-09-04 13:02:29 作者:是梦终空≡°

我有HTML源代码和一个web视图我加载此源:

I have html source and with a WebView i load this source with:

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在SD卡位图或其他,我跟一些导游,但从来没有工作,我该怎么办呢?

Now i want to save this WebView in BitMap or other in the SdCard, i follow some guides but never works, how i can do it?

推荐答案

尝试使用capturePicture功能:

Try to use the capturePicture function:

                    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) {}