android 开发 将view保存为image的实现及将html保存为pdf格式

一、将view保存为image

 
 
public class ImageHelper {


  ///将view保存到bitmap中 public Bitmap createBitmap(View view) { int widthSpec = View.MeasureSpec.makeMeasureSpec(view.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; } /** * 保存bitmap到本地并拷贝并刷新相册 * * @param context 上下文 * @param mBitmap 图片信息 * @param savePath 保存路径(临时) * @param fileName 文件名 * @return */ //public String saveBitmap(Context context,Bitmap mBitmap,String savePath,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, 100, fos); fos.flush(); fos.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); return "保存图片到本地时报错:"+e.getMessage(); } // 其次把文件插入到系统图库 // try { // MediaStore.Images.Media.insertImage(context.getContentResolver(), // filePic.getAbsolutePath(), fileName, null); // //MyToastUtils.showShortToast(context, "保存成功"); // } catch (FileNotFoundException e) { // //MyToastUtils.showShortToast(context, "保存失败"); // e.printStackTrace(); // return "把文件插入到系统图库失败:"+e.getMessage(); // } // 最后通知图库更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(filePic.getPath())))); return "Success"; } }
 
 

 二、将html代码保存为pdf

需要引入三个jar包:itextpdf-5.5.jar、jsoup-1.7.jar、xmlworker-5.5.3.jar

public class PDFHelper {

    ///保存文件的方法
    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, Charset.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 (IOException e) {
            e.printStackTrace();
            return "Error|"+e.getMessage();
        }
    }

    public class MyFont implements FontProvider {
        private static final String FONT_PATH = "/system/fonts/DroidSansFallback.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);
        }
    }
}

 

 

转载于:https://www.cnblogs.com/merlyn/p/9447792.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值