-2

this is my code

btnSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            if (Build.VERSION.SDK_INT > Build.VERSION_CODES.KITKAT) {
                createWebPrintJob(webView);
            }else {


                webView.setWebViewClient(new WebViewClient() {


                    @Override
                    public void onPageFinished(WebView view, String url) {
                        Picture picture = view.capturePicture();
                        Bitmap b = Bitmap.createBitmap(
                                picture.getWidth(), picture.getHeight(), Bitmap.Config.ARGB_8888);
                        Canvas c = new Canvas(b);
                        picture.draw(c);

                        FileOutputStream fos = null;
                        try {
                            fos = new FileOutputStream( "/sdcard/"  + "page.jpg" );
                            if ( fos != null ) {
                                b.compress(Bitmap.CompressFormat.JPEG, 90, fos );
                                fos.close();
                            }
                        }
                        catch( Exception e ) {
                            System.out.println("-----error--"+e);
                        }
                    }
                });


                Toast.makeText(WebViewActivity.this, "Saved", Toast.LENGTH_SHORT).show();
            }

        }
    });

if users sdk>kitkat then webview seved as pdf and if users sdk

part of save pdf worked successfully but save image not work Please help me i need this option.

Mehrdad
  • 1,477
  • 15
  • 17
  • https://stackoverflow.com/questions/20329090/how-to-convert-a-bitmap-to-a-jpeg-file-in-android refre link convert bitmap into image – Divyesh Kalotra Dec 25 '17 at 12:35

2 Answers2

0

I have used this code in one of my project. This works nicely. Try this once.

   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("WIDTH"
                        + bigcanvas.getWidth());
                System.out.println("HEIGHT="
                        + 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();
                    }
                }
            }
        });
Saurav Prakash
  • 588
  • 1
  • 5
  • 26
-1

many time ago i used to do this.

webView.setDrawingCacheEnabled(true);

In onPageFinished()

Bitmap bitmap = Bitmap.createBitmap(view.getDrawingCache());
imageView.setImageBitmap(bitmap);

Hope this helps...

Nikhil Borad
  • 2,065
  • 16
  • 20