3

i have a question about Webview.

It`s my source and result.

private void saveDocumentImage() {

    WebView wv = arrWebView.get(0);

    wv.measure(MeasureSpec.makeMeasureSpec(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
    int y=wv.getHeight();

    Bitmap captureView = Bitmap.createBitmap(wv.getMeasuredWidth(), wv.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
    wv.layout(0, 0, wv.getMeasuredWidth(), wv.getMeasuredHeight());
    Canvas screenShotCanvas = new Canvas(captureView);
    wv.draw(screenShotCanvas);


    if (captureView != null) {
        try {
            String path = Environment.getExternalStorageDirectory().toString();
            OutputStream fOut = null;
            File file = new File(path, "temp");

            if (!file.exists())
                file.mkdirs();

            fOut = new FileOutputStream(path + "/temp/test_0.jpg");
            captureView.compress(Bitmap.CompressFormat.JPEG, 100, fOut);
            wv.setDrawingCacheEnabled(false);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    wv.destroyDrawingCache();
    captureView.recycle();
    captureView=null;
}

and here is same as mine. : https://stackoverflow.com/questions/37023619/capturing-bitmap-android-chromium-webview-returns-content-as-white

Community
  • 1
  • 1
Adrian
  • 301
  • 4
  • 15

1 Answers1

2

U can't take screen shot of web view and surface view because their content is loaded in their holders and holder is not a view. If you find a way then share it. Once i did a little research on it and found this But I haven't tested it yet. try it if it works for you then Accept my answer :D. best Of luck

Adeel Turk
  • 897
  • 8
  • 23
  • 1
    Thanks for ur intention. and I solved it now. lol I need to option 'webview.enableSlowWholeDocumentDraw();' and I found it from android developer. and I got a new problem. lol could u help me?? I want to crop the whole view but It didn`t work... It is my source I added it for cropping. 'bitmap = Bitmap.createBitmap(bitmap, 0,2000, wv.getMeasuredWidth(), 4000);' and I hope the bitmap appear from 2000 to 4000 (x cooperate.), but It appear from 0 to 4000.... – Adrian Jul 11 '16 at 12:48
  • createBitmap(Bitmap source, int x, int y, int width, int height) createBitmap(bitmap, 0,2000, wv.getMeasuredWidth(), 4000); last two parameters will decide the height and width of newly created bitmap u can get your help from http://stackoverflow.com/questions/9747295/crop-particular-part-of-image-in-android http://www.coderzheaven.com/2011/03/15/crop-an-image-in-android/ – Adeel Turk Jul 13 '16 at 05:05
  • especially from this one http://www.coderzheaven.com/2011/03/15/crop-an-image-in-android/ – Adeel Turk Jul 13 '16 at 05:06
  • Thanks for link, Adeel Turk . I solved it but why It was not working well 2 days ago... then I try yesterday it was working well even I didn't fix anything... lol but ur link is really helpful to me. c.f.>, webview.enableSlowWholeDocumentDraw(); <- I solved it using this option. Thanks. – Adrian Jul 13 '16 at 05:11
  • My pleasure Adrian. :D aliens might know this . i am also facing a same issue tomorrow my code was going good but now same code same device but Gurr#%$@@@ :@ :/. – Adeel Turk Jul 13 '16 at 05:23
  • hahaha I can guess what u feel... lol It`s crazy thing. same code and same device but one is nice and the other is bad. lol crazy!!!!!!!!!!! or I`m hottest foolish!! – Adrian Jul 13 '16 at 08:18
  • :D yeah. Best of luck dude coz we need it most of the time – Adeel Turk Jul 13 '16 at 11:01