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

Posted merlyn

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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);
        }
    }
}

 


以上是关于android 开发 将view保存为image的实现及将html保存为pdf格式的主要内容,如果未能解决你的问题,请参考以下文章

将android camera2图像保存为无损PNG

android:怎样将Uri类型的图片数据转换成流

Android View保存为bitmap

Android开发:对View DrawingCache的理解

Android View保存为bitmap

react-native-easy-app 详解与使用之 View,Text,Image,Fl