android 导出图文并茂的html

package com.notice.html;

import android.database.Cursor;

import com.notice.data.MemoData;

import org.json.JSONArray;
import org.json.JSONException;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

/**
 * Created by huangwenli on 2016/12/13.
 */

public class toHtml {
    private static final String mHtmlHead = "<!DOCTYPE html><head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\" /><title>生活帮备忘</title></head><body>"
            + " <caption>  <strong>生活帮备忘导出</strong>  <br />  </caption> ";
    private static final String mHtmlExportTime = "导出日期:date<br><br><br>";
    private static final String mHtmlItemTime = "<h3>dateitem</h3>";
    private static final String mHtmlItemContent = "内容描述:content <br>";
    private static final String mHtmlItemImage = "<p></p><img src=imagepath height=\"600\" width=\"800\"/>";

    private static final String mHtmlEnd = "</body></html>";

    public static void convert(String path, String name[], int num[], float price[]) {
       try {
            String result = mHtmlHead;
            for (int i = 0; i < name.length; i++) {
                String mid = new String(mHtmlItemTime);
                mid = mid.replace("name", name[i]);
                mid = mid.replace("price", "" + num[i]);
                mid = mid.replace("num", "" + price[i]);
                result += mid;
            }

           result += mHtmlEnd;
           saveStringToFile(path, result);
       } catch (Exception e) {
           // TODO Auto-generated catch block
           e.printStackTrace();
       }
    }
    public static void convert(String path, Cursor cursor) {
        try {
            String head = new String(mHtmlHead);
            //head.replace("datexxxx",str);
            String result = head;

            Long timeMillis = System.currentTimeMillis();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日 HH:mm:ss");
            Date curDate = new Date(System.currentTimeMillis());//获取当前时间
            String str = formatter.format(curDate);
            String ExportTime = new String(mHtmlExportTime);
            ExportTime = ExportTime.replace("date",str);
            result += ExportTime;

            if (cursor!=null && cursor.moveToFirst()) {
                do {
                    MemoData data = new MemoData(cursor);
                    String ItemTime = new String(mHtmlItemTime);
                    Date itemDate = new Date(data.updated);//获取当前时间
                    String itemstr = formatter.format(itemDate);
                    ItemTime = ItemTime.replace("dateitem",itemstr);
                    result += ItemTime;

                    String ItemContent = new String(mHtmlItemContent);
                    ItemContent = ItemContent.replace("content",data.content);
                    result += ItemContent;

                    ArrayList<String> mResults_photo = new ArrayList<>();
                    mResults_photo.clear();
                    if(!data.images.isEmpty()){
                        mResults_photo.addAll(data.images);
                        for(int i =0; i < mResults_photo.size();i++)
                        {
                            String image_path_show = mResults_photo.get(i).trim();
                            File imageFile = new File(image_path_show);
                            if (imageFile.exists()) {
                                String ItemImage = new String(mHtmlItemImage);
                                ItemImage = ItemImage.replace("imagepath","file://" + image_path_show);
                                result += ItemImage;
                            }
                        }
                    }

                } while (cursor.moveToNext());
            }


            result += mHtmlEnd;
            saveStringToFile(path, result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    public static boolean saveStringToFile(String path, String content) {
        // FileWriter fw = new FileWriter(path);
        // MTDebug.startCount();
       // ByteBuffer dst = ByteBuffer.allocate(content.length() * 4);
      try {
          FileOutputStream fos = new FileOutputStream(path);
          // 把长宽写入头部
          fos.write(content.getBytes());
          fos.flush();
          fos.close();
      } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
      }
        return true;
    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
CREATE TABLE `boshi_personalcenter` ( `PersonalCenterId` int(11) NOT NULL COMMENT '(id)', `PersonalCenterIntegral` int(11) DEFAULT NULL COMMENT '(个人积分)', `PersonalCenterSZ` varchar(300) DEFAULT NULL COMMENT '(身份证正面)', `PersonalCenterSF` varchar(300) DEFAULT NULL COMMENT '(身份证反面)', `PersonalCenterXZ` varchar(300) DEFAULT NULL COMMENT '(学生证正面)', `PersonalCenterXF` varchar(300) DEFAULT NULL COMMENT '(学生证反面)', `PersonalCenterCredit` int(11) DEFAULT NULL COMMENT '(信用度)', KEY `PersonalCenterId` (`PersonalCenterId`), CONSTRAINT `boshi_personalcenter_ibfk_2` FOREIGN KEY (`PersonalCenterId`) REFERENCES `user` (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; /*Data for the table `boshi_personalcenter` */ insert into `boshi_personalcenter`(`PersonalCenterId`,`PersonalCenterIntegral`,`PersonalCenterSZ`,`PersonalCenterSF`,`PersonalCenterXZ`,`PersonalCenterXF`,`PersonalCenterCredit`) values (1,10,'tu','tu','tu','tu',100),(2,20,'tu','tu','tu','tu',100),(3,30,'tu','tu','tu','tu',100),(1,50,'身份证正面','身份证反面','学生证正面','学生证反面',10),(1,50,'身份证正面','身份证反面','学生证正面','学生证反面',10),(1,50,'身份证正面','身份证反面','学生证正面','学生证反面',10); /*Table structure for table `boshi_profile` */ DROP TABLE IF EXISTS `boshi_profile`; CREATE TABLE `boshi_profile` ( `ProfilePeoId` int(11) DEFAULT NULL COMMENT '(用户ID信息) 外键', `ProfileName` varchar(300) DEFAULT NULL COMMENT '(姓名)', `ProfilePhp` varchar(300) DEFAULT NULL COMMENT '(联系方式)', `ProfileAddress` varchar(300) DEFAULT NULL COMMENT '(地址)', `ProfileId` int(11) NOT NULL AUTO_INCREMENT COMMENT '(地址id)', PRIMARY KEY (`ProfileId`), KEY `ProfilePeoId` (`ProfilePeoId`), CONSTRAINT `boshi_profile_ibfk_1` FOREIGN KEY (`ProfilePeoId`) REFERENCES `user` (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=21 DEFAULT CHARSET=utf8; /*Data for the table `boshi_profile` */ insert into `boshi_profile`(`ProfilePeoId`,`ProfileName`,`ProfilePhp`,`ProfileAddress`,`ProfileId`) values (2,'赵六','1366663435','合肥',2),(2,'斩三','13214326346','合肥',5),(2,'李四','12334453654','北大青鸟',6),(2,'李四','1888555555','包河区',7),(2,'赵六','1366663435','合肥',8),(2,'赵六','12354697883','上海市',9),(2,'赵六','12354697883','上海市',10),(2,'赵六','12354697883','上海市',11),(1,'张三','17776462665','合肥市包河区',12),(1,'王五','1423534534654','合肥市',13),(2,'赵六','1366663435','合肥',14),(1,'赵六','17776462665','北大青鸟',20); /*Table structure for table `boshi_putaway` */ DROP TABLE IF EXISTS `boshi_putaway`;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值