Android Webview Save as Picture

Source: Internet
Author: User

/** * @param view, the view want to screenshot, such as WebView, etc. * @param width, screenshot ' s width. * @param height, screenshot ' s height. * @param scroll, true if you want to start capture from currently scroll position. * @param config, the bitmap.config, such as argb_8888, etc. * @return The screenshot ' s Bitmap */public static Bitmap cap         Ture (view view, float width, float height, boolean scroll, Bitmap.config Config) {if (!view.isdrawingcacheenabled ()) {    View.setdrawingcacheenabled (TRUE);    } Bitmap Bitmap = Bitmap.createbitmap ((int) width, (int) height, config);    Bitmap.erasecolor (Color.White);    Canvas canvas = new canvas (bitmap);    int left = View.getleft ();    int top = View.gettop ();        if (scroll) {left = View.getscrollx ();    top = view.getscrolly ();    } int status = Canvas.save ();    Canvas.translate (-left,-top);    Float scale = width/view.getwidth ();    Canvas.scale (scale, scale, left, top);    View.draw (canvas); Canvas.reStoretocount (status);    Paint Alphapaint = new paint ();    Alphapaint.setcolor (color.transparent);    Canvas.drawrect (0f, 0f, 1f, height, alphapaint);    Canvas.drawrect (width-1f, 0f, width, height, alphapaint);    Canvas.drawrect (0f, 0f, width, 1f, alphapaint);    Canvas.drawrect (0f, height-1f, width, height, alphapaint);    Canvas.setbitmap (NULL); return bitmap;}


public static String screenshot (context context, Bitmap Bitmap, String name) {if (Bitmap = = null) {RET        Urn null;        } File dir = environment.getexternalstoragepublicdirectory (environment.directory_pictures);        if (name = = NULL | | Name.trim (). IsEmpty ()) {name = String.valueof (System.currenttimemillis ());        } name = Name.trim ();        int count = 0;        File File = new file (dir, name + ". png");            while (File.exists ()) {count++;        File = new file (dir, name + "." + Count + ". png");            } try {FileOutputStream stream = new FileOutputStream (file);            Bitmap.compress (Bitmap.CompressFormat.PNG, N, Stream);            Stream.flush ();            Stream.Close ();            Context.sendbroadcast (New Intent (Intent.action_media_scanner_scan_file, Uri.fromfile (FILE));        return File.getabsolutepath ();        } catch (Exception e) {return null; }    }


Android Webview Save as Picture

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.