android html to bitmap

Solutions on MaxInterview for android html to bitmap by the best coders in the world

showing results for - "android html to bitmap"
Angelo
01 Apr 2018
1public Bitmap getBitmap(final WebView w, int containerWidth, int containerHeight, final String baseURL, final String content) {
2    final CountDownLatch signal = new CountDownLatch(1);
3    final Bitmap b = Bitmap.createBitmap(containerWidth, containerHeight, Bitmap.Config.ARGB_8888);
4    final AtomicBoolean ready = new AtomicBoolean(false); 
5    w.post(new Runnable() {
6
7        @Override
8        public void run() {
9            w.setWebViewClient(new WebViewClient() {
10                @Override
11                public void onPageFinished(WebView view, String url) {
12                    ready.set(true);
13                }
14            });
15            w.setPictureListener(new PictureListener() {
16                @Override
17                public void onNewPicture(WebView view, Picture picture) {
18                    if (ready.get()) {
19                        final Canvas c = new Canvas(b);
20                        view.draw(c);
21                        w.setPictureListener(null);
22                        signal.countDown();
23                    }
24                }
25            });
26            w.layout(0, 0, rect.width(), rect.height());
27            w.loadDataWithBaseURL(baseURL, content, "text/html", "UTF-8", null);
28        }});
29    try {
30        signal.await();
31    } catch (InterruptedException e) {
32        e.printStackTrace();
33    }
34    return b;
35}
36