如何从Android应用程序中取出打印输出?

时间:2014-03-24 07:12:50

标签: android printing

嗨在我的应用程序中我想打印出我的webview页面,即html page.can任何人都告诉我我犯了什么错误。我的问题是代码在emultor中运行而不是在任何其他设备中运行。

MainActivity.java:

package com.example.print1;

import android.os.Build;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;

import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.content.Context;

@SuppressLint("NewApi")
public class MainActivity extends Activity {

    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.web_print);

            WebView webView = new WebView(this);
            webView.setWebViewClient(new WebViewClient() {

               public boolean shouldOverrideUrlLoading(WebView view, 
                              String url) 
           {
                    return false;     
               }

               @Override
               public void onPageFinished(WebView view, String url) {
                    createWebPrintJob(view);
                    myWebView = null;
                }
        });

        String htmlDocument = 
                   "<html><body><h1>Android Print Test</h1><p>" 
                + "This is some sample content.</p></body></html>";

        webView.loadDataWithBaseURL(null, htmlDocument, 
                   "text/HTML", "UTF-8", null);

        myWebView = webView;
    }
    @TargetApi(19)
    @SuppressLint("NewApi")
    private void createWebPrintJob(WebView webView) {

        PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);

        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

        String jobName = getString(R.string.app_name) + " Print Test";

        printManager.print(jobName, printAdapter,new PrintAttributes.Builder().build());
    }
}

WebPrintActivity.java

package com.example.print1;
import android.os.Bundle;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;

public class WebPrintActivity extends Activity {

    private WebView myWebView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        myWebView = (WebView) findViewById(R.id.webView1);

        myWebView.loadUrl("http://developer.android.com/google/index.html");
    }
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
               case R.id.action_print:
                createWebPrintJob(myWebView);
                return true;
            default:
                return super.onOptionsItemSelected(item);
        }
    }
    private void createWebPrintJob(WebView webView) {

        PrintManager printManager = (PrintManager) this
              .getSystemService(Context.PRINT_SERVICE);

        PrintDocumentAdapter printAdapter = 
                 webView.createPrintDocumentAdapter();

        String jobName = getString(R.string.app_name) + 
                            " Print Test";

        printManager.print(jobName, printAdapter,
               new PrintAttributes.Builder().build());
    }
}

上面的代码在模拟器中运行但是在emultor中没有wifi,如果在Android设备中运行相同的代码意味着它显示错误并且它没有显示正确的输出,我也想从android获取打印输出。 感谢。

1 个答案:

答案 0 :(得分:1)

打印框架仅适用于19级及以上(android kitkat)。

 private void createWebPrintJob(WebView webView) {
  if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
 {

        PrintManager printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);

        PrintDocumentAdapter printAdapter = webView.createPrintDocumentAdapter();

        String jobName = getString(R.string.app_name) + " Print Test";

        printManager.print(jobName, printAdapter,new PrintAttributes.Builder().build());
 }
    }