webView.getMeasuredHeight()); webView.setDrawingCacheEnabled(true); webView.buildDrawingCache(); Bitmap bitmap = Bitmap.createBitmap(webView.getMeasuredWidth(), webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
webView.getMeasuredHeight()); webView.setDrawingCacheEnabled(true); webView.buildDrawingCache(); Bitmap bm = Bitmap.createBitmap(webView.getMeasuredWidth(), webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
public Bitmap getBitmapFromWebView(WebView webView) { webView.setDrawingCacheEnabled(true); webView.buildDrawingCache(true); Bitmap bitmap = Bitmap.createBitmap(webview.getDrawingCache()); webView.setDrawingCacheEnabled(false); return bitmap; }
/** * 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; } }
public static Bitmap screenshot2(WebView webView) { 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 bitmap = Bitmap.createBitmap(webView.getMeasuredWidth(), webView.getMeasuredHeight(), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(bitmap); Paint paint = new Paint(); int iHeight = bitmap.getHeight(); canvas.drawBitmap(bitmap, 0, iHeight, paint); webView.draw(canvas); return bitmap; }