Which can replace capturePicture function

前端 未结 3 674
终归单人心
终归单人心 2020-11-27 17:56

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 funct

3条回答
  •  暖寄归人
    2020-11-27 18:17

    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

    
    
    

提交回复
热议问题