[英]How do I display and print raw HTML on an Android device?
I have a java application that I'm porting over to android. 我有一个要移植到android的java应用程序。 One function I have is to export a summary of the data to HTML that is displayed in the local browser. 我要执行的一项功能是将数据摘要导出到本地浏览器中显示的HTML。 How would I mimic this functionality on the Android platform? 我将如何在Android平台上模仿此功能? The intention is to allow the user to view the data easily and save/export/print as they want to. 目的是允许用户轻松查看数据并根据需要保存/导出/打印。
Here's my regular desktop Java version: 这是我的常规桌面Java版本:
String html = "<html><head><style type=\"text/css\">.pagebreak {page-break-after: always;}.smallFont{font-size:10px}" + fancyCss + "</style>" + internationalCharacters + "</head><body>" + content + "</body></html>";
try {
File file = File.createTempFile(filename, ".html");
FileOutputStream stream = new FileOutputStream(file);
stream.write(html.getBytes("UTF-8"));
stream.flush();
stream.close();
Desktop.getDesktop().open(file);
} catch (IOException e) {
e.printStackTrace();
}
Simple answer is to use a webview. 简单的答案是使用网络视图。
include a webview in your xml file 在您的xml文件中包含一个webview
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<WebView
android:id="@+id/myWebView"
android:layout_alignParentLeft="true"
android:layout_below="@+id/print_button"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button
android:text="Print"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/print_button" />
</RelativeLayout>
you can set html code to the webview in your java class. 您可以在Java类中将html代码设置为webview。
String html = "<html><body>Hello, World!</body></html>";
String mime = "text/html";
String encoding = "utf-8";
final WebView myWebView = (WebView)this.findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.loadDataWithBaseURL(null, html, mime, encoding, null);
Button printButton = (Button)findViewById(R.id.print_button);
printButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
createWebPrintJob(myWebView);
}
});
And use following method to do the print job 并使用以下方法进行打印
private void createWebPrintJob(WebView webView) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
PrintManager printManager = (PrintManager) this
.getSystemService(Context.PRINT_SERVICE);
PrintDocumentAdapter printAdapter =
null;
printAdapter = webView.createPrintDocumentAdapter("MyDocument");
String jobName = getString(R.string.app_name) + " Print Test";
printManager.print(jobName, printAdapter,
new PrintAttributes.Builder().build());
}
else{
Toast.makeText(MainActivity.this, "Print job has been canceled! ", Toast.LENGTH_SHORT).show();
}
}
NOTE : the above method is working only on the diveces with Lollipop or later. 注意:以上方法仅适用于棒棒糖或更高版本的潜水者。 Anyway you can save your webview as a bitmap if you want. 无论如何,您都可以根据需要将Web视图另存为位图。
The code is tested and working. 该代码已经过测试并且可以正常工作。 Hope this helps you 希望这对您有帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.