Android自動生成htmlレポートの画像


webviewのcatchを使って、まずデータをファイルに生成して、webviewで読みます.スクリーンショットはjpgを生成します.
package com.test.html5;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Picture;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class HTML5Activity extends Activity implements OnClickListener{
	/** Called when the activity is first created. */
	private WebView webview;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// 2.2                ========start=========
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder().detectDiskReads().detectDiskWrites()
				.detectNetwork() // or
									// .detectAll()
									// for
									// all
									// detectable
									// problems
				.penaltyLog().build());
		StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectLeakedSqlLiteObjects()
				.detectLeakedSqlLiteObjects().penaltyLog().penaltyDeath().build());

		webview = (WebView) findViewById(R.id.web_3d);
		//   WebView  ,    Javascript  
		WebSettings settings = webview.getSettings();
		settings.setJavaScriptEnabled(true);
		settings.setPluginState(WebSettings.PluginState.ON);
		settings.setPluginsEnabled(true);
		webview.setWebViewClient(new MvtFlashWebViewClient());
		//    
		webview.setDrawingCacheEnabled(true);
		//        

		settings.setLoadWithOverviewMode(true);
		String url = testCreateHTML();//          
		webview.loadUrl(url);
		webview.setOnTouchListener(new OnTouchListenerHTML5());
		
	}

	public class OnTouchListenerHTML5 implements OnTouchListener {

		@Override
		public boolean onTouch(View v, MotionEvent event) {
			// TODO Auto-generated method stub
			if (event.getAction() == MotionEvent.ACTION_DOWN) {
				webview.loadUrl("javascript:canvasMouseDown(" + event.getX() + "," + event.getY() + ")");
				return true;
			} else if (event.getAction() == MotionEvent.ACTION_MOVE) {
				webview.loadUrl("javascript:canvasMouseMove(" + event.getX() + "," + event.getY() + ")");
				return true;
			} else if (event.getAction() == MotionEvent.ACTION_UP) {
				// webview.loadUrl("javascript:canvasMouseDown("+event.getX()+","+event.getY()+")");
				return true;
			}
			return false;
		}

	}

	public String testCreateHTML() {
		String name[] = { "  ", "  ", "  " };
		int num[] = { 1, 4, 1 };
		float price[] = { 100, 25, 80 };
		String path = "/sdcard/savehtml.html";
		toHtml.convert(path, name, num, price);
		return "file:///mnt" + path;
	}

	public void saveWebviewPic() {
		Picture picture = webview.capturePicture();
		Bitmap bmp = Bitmap.createBitmap(picture.getWidth(), picture.getHeight(), Bitmap.Config.RGB_565);
		Canvas c = new Canvas(bmp);
		picture.draw(c);
		savePic("/sdcard/html.jpg", bmp, 1);
	}

	//     
	public static boolean savePic(String path, Bitmap bmp, int quality) {
		if (bmp == null || bmp.isRecycled()) {
			return false;
		}
		File myCaptureFile = new File(path);
		try {
			BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(myCaptureFile));
			if (quality == 1) {// jpg
				bmp.compress(Bitmap.CompressFormat.JPEG, 85, bos);
			} else if (quality == 2) {// png
				bmp.compress(Bitmap.CompressFormat.PNG, 100, bos);
			} else if (quality == 3) {//     
				bmp.compress(Bitmap.CompressFormat.JPEG, 75, bos);
			} else if (quality == 5) {// jpg
				bmp.compress(Bitmap.CompressFormat.JPEG, 100, bos);
			}
			try {
				bos.flush();
				bos.close();
				// writeEixf(path);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return true;
	}

	// Web  
	private class MvtFlashWebViewClient extends WebViewClient {
		@Override
		public void onPageFinished(WebView view, String url) {
			// TODO Auto-generated method stub
			
			
			super.onPageFinished(view, url);
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId()==R.id.btn_save){
			saveWebviewPic();
		}
	}

}
package com.test.html5;

import java.io.FileOutputStream;
import java.io.IOException;

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></table><p> </p>"
			+ "<table width=\"566\" height=\"36\" border=\"1\">  <caption>  <strong>  </strong>  <br />    :a      : b <br /> </caption> "
			+ "<tr> <td height=\"30\">  </td>    <td>  </td>    <td>  </td>  </tr>";
	private static final String mHtmlItem = "<tr> <td height=\"30\">name</td>    <td>price</td>    <td>num</td>  </tr>";
	private static final String mHtmlEnd = "</table></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(mHtmlItem);
				mid = mid.replace("name", name[i]);
				mid = mid.replace("price", "" + num[i]);
				mid = mid.replace("num", "" + price[i]);
				result += mid;
			}
			for (int i = 0; i < 20; i++) {
				String mid = new String(mHtmlItem);
				mid = mid.replace("name", name[0]+i);
				mid = mid.replace("price", "" + num[0]);
				mid = mid.replace("num", "" + price[0]);
				result += mid;
			}
			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;
	}
}