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.FileOutputSt
·
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)