@Override public void getSnapshot(OnSnapshotReadyListener listener) { boolean isDrawingCacheEnabled = webView.isDrawingCacheEnabled(); webView.setDrawingCacheEnabled(true); // copy to a new bitmap, otherwise the bitmap will be // destroyed when the drawing cache is destroyed // webView.getDrawingCache can return null if drawing cache is disabled or if the size is 0 Bitmap bitmap = webView.getDrawingCache(); Bitmap newBitmap = null; if (bitmap != null) { newBitmap = bitmap.copy(Bitmap.Config.RGB_565, false); } webView.destroyDrawingCache(); webView.setDrawingCacheEnabled(isDrawingCacheEnabled); listener.onSnapshotReady(newBitmap); } }
/** * Create a picture out of {@link WebView}'s whole content * * @param webView The WebView to get contents from * @return A {@link Bitmap} or null */ @Nullable public static Bitmap getBitmapFromWebView(WebView webView) { try { //Measure WebView's content int widthMeasureSpec = View.MeasureSpec.makeMeasureSpec(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED); int heightMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); webView.measure(widthMeasureSpec, heightMeasureSpec); webView.layout(0, 0, webView.getMeasuredWidth(), webView.getMeasuredHeight()); //Build drawing cache and store its size webView.buildDrawingCache(); int measuredWidth = webView.getMeasuredWidth(); int measuredHeight = webView.getMeasuredHeight(); //Creates the bitmap and draw WebView's content on in Bitmap bitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); canvas.drawBitmap(bitmap, 0, bitmap.getHeight(), new Paint()); webView.draw(canvas); webView.destroyDrawingCache(); return bitmap; } catch (Exception | OutOfMemoryError e) { e.printStackTrace(); return null; } }
@Override public void onDestroy() { mWebView.stopLoading(); mWebView.destroyDrawingCache(); mWebView.removeAllViews(); mWebView.setVisibility(View.GONE); mWebView.destroy(); super.onDestroy(); }
webview.setWebChromeClient(null); webview.setWebViewClient(null); webview.destroyDrawingCache(); webview.destroy(); webview = null;
webview.setWebChromeClient(null); webview.setWebViewClient(null); webview.destroyDrawingCache(); webview.destroy(); webview = null;
webView.destroyDrawingCache(); webView.loadDataWithBaseURL(Constants.BASE_URL, versionHelper.getReleaseNotes(false), "text/html", "utf-8", null);