Android Development saves view as an implementation of image and saves HTML in PDF format

Source: Internet
Author: User

One, save view as Image

public class Imagehelper {
  Save view to Bitmap public Bitmap createbitmap (view view) {int widthspec = View.MeasureSpec.makeMeasureSpec (vi Ew.getlayoutparams (). width, View.MeasureSpec.EXACTLY); int heightspec = View.MeasureSpec.makeMeasureSpec (View.getlayoutparams (). height, View.MeasureSpec.EXACTLY); View.measure (Widthspec, Heightspec); int measurewidth = View.getmeasuredwidth (); int measureheight = View.getmeasuredheight (); View.layout (0, 0, measurewidth, measureheight); int width = view.getwidth (); int height = view.getheight (); Bitmap Bitmap = bitmap.createbitmap (width, height, Bitmap.Config.ARGB_8888); Canvas canvas = new canvas (bitmap); View.draw (canvas); return bitmap; }/** * Save bitmap to local and copy and Refresh album * * @param context context * @param mbitmap picture information * @param savepath save Path ( TEMPORARY) * @param filename * @return * *//public String savebitmap (Context context,bitmap mbitmap,string s Avepath,sTring fileName) {public String savebitmap (Context context,bitmap mbitmap,string savepath) {File filepic; try {filepic = new File (Savepath); if (!filepic.exists ()) {Filepic.getparentfile (). Mkdirs (); Filepic.createnewfile (); } fileoutputstream fos = new FileOutputStream (filepic); Mbitmap.compress (Bitmap.CompressFormat.JPEG, N, FOS); Fos.flush (); Fos.close (); } catch (IOException e) {//TODO auto-generated catch block E.printstacktrace (); Return "Save picture to local times wrong:" +e.getmessage (); }//Next insert the file into the system library//try {//MediaStore.Images.Media.insertImage (Context.getcontentresolver (),// Filepic.getabsolutepath (), fileName, NULL);////mytoastutils.showshorttoast (context, "Save succeeded"); } catch (FileNotFoundException e) {////mytoastutils.showshorttoast (context, "Save Failed");//E.printstacktrace ();//return "Insert file into system Library failed:" +e.getmessage ();//}//Last Notification Gallery Update Context.sendbroadcast (new Intent (Intent.action_media_scanner_scan_file, Uri.fromfile (new FILE (f Ilepic.getpath ()))); return "Success"; }}

Second, save the HTML code as a PDF

Need to introduce three jar packages: Itextpdf-5.5.jar, Jsoup-1.7.jar, Xmlworker-5.5.3.jar

public class Pdfhelper {///Save File Method public String createpdf (string rawhtml, String fileName, Contextwrapper context)        {File File = new file (fileName);            try{Document document = new document ();            PDFWriter writer = pdfwriter.getinstance (document, new FileOutputStream (file));            Document.open ();            HTML String htmltext = Jsoup.clean (rawhtml, whitelist.relaxed ());            InputStream InputStream = new Bytearrayinputstream (Htmltext.getbytes ()); PDF xmlworkerhelper.getinstance (). parsexhtml (writer, document, InputStream, NULL, Charse            T.defaultcharset (), New MyFont ());            Document.close ();        Return "";            } catch (FileNotFoundException e) {e.printstacktrace (); Return "error|"        +e.getmessage ();            } catch (Documentexception e) {e.printstacktrace (); Return "error|"        +e.getmessage (); } catch (IOExceptione) {e.printstacktrace (); Return "error|"        +e.getmessage (); }} public class MyFont implements Fontprovider {private static final String Font_path = "/system/fonts/droi        Dsansfallback.ttf ";        private static final String Font_alias = "My_font";        Public MyFont () {fontfactory.register (Font_path, Font_alias);                            } @Override Public Font getfont (string fontname, String encoding, Boolean embedded,                    float size, int style, Basecolor color) {return Fontfactory.getfont (Font_alias, Basefont.identity_h,        basefont.embedded, size, style, color);        } @Override public boolean isregistered (String name) {return name.equals (Font_alias); }    }}

Android Development saves view as an implementation of image and saves HTML in PDF format

Related Article

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.