Which can replace capturePicture function

19,241

Solution 1

I finally found out the solution.

Some of codes

public class WebViewActivity extends Activity {

    private static WebView webView;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.webview);

        webView = (WebView) findViewById(R.id.webView1);
        webView.loadUrl("http://developer.android.com/reference/packages.html");
//      webView.loadUrl("http://developer.android.com/training/basics/firstapp/creating-project.html");

        webView.setWebViewClient(new WebViewClient() {

            public void onPageFinished(WebView view, String url) {
                // do your stuff here
                webView.measure(MeasureSpec.makeMeasureSpec(
                        MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED),
                        MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
                webView.layout(0, 0, webView.getMeasuredWidth(),
                        webView.getMeasuredHeight());
                webView.setDrawingCacheEnabled(true);
                webView.buildDrawingCache();
                Bitmap bm = Bitmap.createBitmap(webView.getMeasuredWidth(),
                        webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);

                Canvas bigcanvas = new Canvas(bm);
                Paint paint = new Paint();
                int iHeight = bm.getHeight();
                bigcanvas.drawBitmap(bm, 0, iHeight, paint);
                webView.draw(bigcanvas);
                System.out.println("1111111111111111111111="
                        + bigcanvas.getWidth());
                System.out.println("22222222222222222222222="
                        + bigcanvas.getHeight());

                if (bm != null) {
                    try {
                        String path = Environment.getExternalStorageDirectory()
                                .toString();
                        OutputStream fOut = null;
                        File file = new File(path, "/aaaa.png");
                        fOut = new FileOutputStream(file);

                        bm.compress(Bitmap.CompressFormat.PNG, 50, fOut);
                        fOut.flush();
                        fOut.close();
                        bm.recycle();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
        });
    }
}

The layout.xml

<?xml version="1.0" encoding="utf-8"?>
<WebView  xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
/>

Solution 2

Use draw() method of WebView

Example :

ImageView imageview;
WebView webview;
class Background extends AsyncTask<Void, Void, Bitmap>
{
    @Override
    protected Bitmap doInBackground(Void... params)
    {
        try
        {
            Thread.sleep(2000);
            Bitmap bitmap = Bitmap.createBitmap(webview.getWidth(), webview.getHeight(), Config.ARGB_8888);
            Canvas canvas = new Canvas(bitmap);
            webview.draw(canvas);
            return bitmap;
        }
        catch (InterruptedException e){}
        catch (Exception e){}
        return null;
    }
    @Override
    protected void onPostExecute(Bitmap result)
    {
        imageview.setImageBitmap(result);
    }
}


webview.setWebChromeClient(new WebChromeClient()
{
    public void onProgressChanged(WebView view, int progress)  
    {
        if(progress==100)
            new Background().execute();
    }
});
Share:
19,241

Related videos on Youtube

lolyoshi
Author by

lolyoshi

Updated on September 29, 2022

Comments

  • lolyoshi
    lolyoshi over 1 year

    I have a question. At this time, the capturePicture of WebView is deprecated.

    I want to ask if there is a way to replace the function. I meant it can capture entire of the webview (not only the view is displayed)

    Thanks

  • Yogesh Lakhotia
    Yogesh Lakhotia about 10 years
    getting error java.lang.IllegalArgumentException: width and height must be > 0 please help me
  • lolyoshi
    lolyoshi about 10 years
    @YogeshLakhotia: Be sure that the webview loaded completely before you get height or width. I used AsyncTask. I don't know why but onPageFinished can run before your webview is loaded completely. I got the same problem. I don't see your code, so I just suggest you some cases.
  • user512
    user512 about 9 years
    i tried this but webview not loading completely.. do u have any idea?
  • user512
    user512 about 9 years
    i tried this code webview loading completely but after saving to file its not loading completely.. do u have any idea? please help..
  • lolyoshi
    lolyoshi about 9 years
    @user512 Well, it seems that you have to triggered in the webview once. Try to set a "TakePhoto" button on top bar, and it's just visible when the webview load completely. After that, touch the button and check if you can take the photo of the webview
  • userAndroid
    userAndroid about 8 years
    worked on 7-8 ways , but only this method worked on my case :)
  • Stan
    Stan about 8 years
    Why do you call bigcanvas.drawBitmap(bm, 0, iHeight, paint)? This draws the bitmap on itself.
  • prasanthMurugan
    prasanthMurugan about 7 years
    this code is not working for me i have enabled setBuiltInZoomControls setSupportZoom it create a vertical image height 800 dp but with width 50dp ?
  • prasanthMurugan
    prasanthMurugan about 7 years
    @YogeshLakhotia@Stan@user512
  • Martin Braun
    Martin Braun over 3 years
    This illustrates, why marking capturePicture deprecated was a mistake.