一、将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;
}
//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() );
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);
}
}
}