【问题标题】:How do I display and print raw HTML on an Android device?如何在 Android 设备上显示和打印原始 HTML?
【发布时间】:2016-10-04 13:39:02
【问题描述】:

我有一个 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();
}

【问题讨论】:

标签: java android html printing webview


【解决方案1】:

简单的答案是使用 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>

您可以在您的 java 类中为 webview 设置 html 代码。

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 或更高版本的潜水。无论如何,如果需要,您可以将 web 视图保存为位图。
代码已经过测试并且可以工作。希望对你有帮助

【讨论】:

  • 终于有时间做这个了。所以,从技术上讲,是的,它会按要求显示 HTML。我现在的问题是 webview 的功能为零。我希望至少有一些标准的浏览器东西,比如打印。可以吗?或者我可以将 html 推送到标准浏览器吗?
  • @Chris.Brown.SPE 您可以打印网页视图。请查看更新的答案。也可以通过将 webview 捕获为位图将其保存为图像
猜你喜欢
  • 2017-03-11
  • 2021-01-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-11-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多