Android中保存html格式的文件

  最近根据需求做了一个基于Android的输出Html的报告,其中用到了一个JAR包——JSOUP。具体如下:在dreamweaver中做好一个html的报告单(当然要考虑其浏览器的兼容性),然后将它放入Android中的assets中,通过Jsoup找到相应的节点,将内容填进报告单中,具体代码如下:

<span style="font-size:18px;">package com.shao.report;

import java.io.ByteArrayOutputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.text.format.Time;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

	Button saveBtn;
	Document doc;
	byte[] data;
	int lenth;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		setListener();
	}

	public void initView() {
		saveBtn = (Button) findViewById(R.id.save_html);
	}
        //保存html格式的文档
	@SuppressLint("SimpleDateFormat")
	public void setListener() {
		
		saveBtn.setOnClickListener(new OnClickListener() {

			@SuppressLint("SdCardPath")
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				try {
					InputStream inStream = getAssets().open("report.html");
					ByteArrayOutputStream outstream = new ByteArrayOutputStream();
					lenth = inStream.available();
					if (inStream != null) {
						byte[] buffer = new byte[lenth];
						int len = -1;
						while ((len = inStream.read(buffer)) != -1) {
							outstream.write(buffer, 0, len);
						}
						outstream.close();
						inStream.close();
						data = outstream.toByteArray();

					}
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				String text = new String(data, 0, lenth);
				doc = Jsoup.parse(text);
				Element sample = doc.getElementById("sample");
				Element source = doc.getElementById("source");
				Element instrument = doc.getElementById("instrument");
				Element deviceid = doc.getElementById("deviceid");
				Element power = doc.getElementById("power");
				Element wavelength = doc.getElementById("wavelength");
				Element time = doc.getElementById("time");
				Element software = doc.getElementById("software");
				Element unit = doc.getElementById("unit");
				Element testperson = doc.getElementById("testperson");
				sample.text("数学");
				source.text("物理");
				instrument.text("化学");
				deviceid.text("生物");
				power.text("政治");
				wavelength.text("数据结构");
				time.text("语文");
				software.text("英语");
				unit.text("张家口");
				testperson.text("小小");
				// Elements pngs = doc.select("img[src$=report_a.png]");// 所有引用png 图片的元素
				
				try {
				   Time t=new Time();
			           t.setToNow();
			           int year=t.year;
			           int month=t.month;
			           int day=t.monthDay;
			           int hour=t.hour;
			           int minute=t.minute;
			           int second=t.second;

			           String filename=""+year+month+day+hour+minute+second;
					String path = "/sdcard/"+filename+"test.html";
					FileOutputStream fos = new FileOutputStream(path, true);
					OutputStreamWriter osw = new OutputStreamWriter(fos,
							"utf-8");
					String title = "<!DOCTYPE html PUBLIC\" -//W3C//DTD XHTML 1.0 Transitional//EN\" \"quot http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">";
					StringBuffer htmlString = new StringBuffer();
					htmlString.append(title);
					htmlString.append(doc.html());
					osw.write(htmlString.toString());
					osw.close();
				} catch (FileNotFoundException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (UnsupportedEncodingException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}</span>
           源码下载: Report.zip

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值