如何在Android设备上显示和打印原始HTML?

时间:2016-10-04 13:39:02

标签: java android html printing webview

我有一个java应用程序,我正在移植到android。我的一个功能是将数据摘要导出到本地浏览器中显示的HTML。我将如何在Android平台上模仿此功能?目的是让用户轻松查看数据并保存/导出/打印。

这是我的常规桌面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();
}

1 个答案:

答案 0 :(得分:2)

简单回答是使用webview。

在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>

您可以将html代码设置为java类中的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);
    }
});

并使用以下方法执行打印作业

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();
    }

}

注意:上述方法仅适用于与Lollipop或更高版本的转移。无论如何,如果你愿意,你可以将你的webview保存为位图 代码经过测试和运行。希望这可以帮助你