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
Beginning October 5, 2021, Facebook Login will no longer support using Android embedded browsers (WebViews) for logging in users.
The loadUrl() and loadData() methods of Android WebView class are used to load and display web page.
The WebView class is an extension of Android's View class that allows you to display web pages as a part of your activity layout. It does not include any features of a fully developed web browser, such as navigation controls or an address bar. All that WebView does, by default, is show a web page.
To open links clicked by the user, simply provide a WebViewClient for your WebView, using setWebViewClient(). Demo of creating an application to open any URL inside the application and clicking on any link from that URl. should not open Native browser but that URL should open in the same screen.
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" />
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(); } });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With