0

我正在设计用于从 android 打印到无线本地打印机的 android 应用程序。应用程序正在运行,但在 logcat 中显示“打印机无法解析或支持”错误。我使用的打印机是带无线连接的Brother QL-710W 。这背后的原因是什么。

代码如下。

主要活动

import android.os.Bundle;
import android.print.PrintDocumentAdapter;
import android.print.PrintManager;
import android.print.PrinterInfo;
import android.support.v4.print.PrintHelper;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.Menu;
import android.view.View;

public class MainActivity extends Activity 
{

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu) 
    {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public void onClick(View v)
    {
        if(v.getId()==R.id.button1)
        {
            CustomDocumentAdaptor adaptor=new CustomDocumentAdaptor(getApplicationContext(), BitmapFactory.decodeResource(getResources(), R.drawable.dmg), "Mandar", "Kuch bhi");
            PrintPdf("test.pdf", adaptor);
        }
    }

    private void PrintPdf(String jobName, PrintDocumentAdapter adaptor)
    {
        if(PrintHelper.systemSupportsPrint())
        {
            PrintManager manager=(PrintManager)getSystemService(Context.PRINT_SERVICE);

            manager.print(jobName, adaptor, null);
        }
    }

用于pdf打印的CustomDocumentAdaptor如下

import java.io.FileOutputStream;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.pdf.PdfDocument;
import android.os.Bundle;
import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor;
import android.print.PageRange;
import android.print.PrintAttributes;
import android.print.PrintDocumentAdapter;
import android.print.PrintDocumentInfo;
import android.print.pdf.PrintedPdfDocument;

public class CustomDocumentAdaptor extends PrintDocumentAdapter
{
    private Paint paint=new Paint();
    private Context myContext;
    private Bitmap myBitmap;
    private String myName,message;
    PrintedPdfDocument pdfDocument;

    public CustomDocumentAdaptor(Context context,Bitmap bitmap,String Name,String msg) 
    {
        myContext=context;
        myBitmap=bitmap;
        myName=Name;
        message=msg;
    }

    @Override
    public void onLayout(PrintAttributes arg0, PrintAttributes arg1, CancellationSignal arg2, LayoutResultCallback arg3, Bundle arg4) 
    {
        pdfDocument=new PrintedPdfDocument(myContext, arg1);

        if(arg2.isCanceled())
        {
            arg3.onLayoutCancelled();
            return;
        }

        int pages=1;

        PrintDocumentInfo info=new PrintDocumentInfo.Builder("test.pdf")
                                .setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT)
                                .setPageCount(pages).build();

        arg3.onLayoutFinished(info, true);

    }

    @Override
    public void onWrite(PageRange[] arg0, ParcelFileDescriptor arg1,
            CancellationSignal arg2, WriteResultCallback arg3) 
    {
        if(pdfDocument==null)
        {
            return;
        }

        PdfDocument.Page page=pdfDocument.startPage(0);

        if(arg2.isCanceled())
        {
            arg3.onWriteCancelled();
            pdfDocument.close();
            pdfDocument=null;
            return;
        }

        onDraw(page.getCanvas());
        pdfDocument.finishPage(page);

        try
        {
            pdfDocument.writeTo(new FileOutputStream(arg1.getFileDescriptor()));
        }
        catch(Exception e)
        {

        }
        finally
        {
            pdfDocument.close();
            pdfDocument=null;
        }

        arg3.onWriteFinished(arg0);
    }

    public void onDraw(Canvas canvas)
    {
        paint.setTextSize(14);
        paint.setColor(Color.BLACK);

        canvas.drawText(myName, 25, 25, paint);

        canvas.drawBitmap(myBitmap,35,55 , paint);
    }

}

logcat如下

01-17 16:18:23.159: D/PRINT_LIB(20142): Response from /192.168.1.1:161
01-17 16:18:23.159: E/SnmpDiscovery(20142): Error while reading SNMP response.
01-17 16:18:23.159: E/SnmpDiscovery(20142): java.net.ProtocolException: SNMP error: 2
01-17 16:18:23.159: E/SnmpDiscovery(20142):     at com.hp.mobileprint.discoveryservice.parsers.DeviceStatus.<init>(DeviceStatus.java:117)
01-17 16:18:23.159: E/SnmpDiscovery(20142):     at com.hp.mobileprint.discoveryservice.SnmpDiscovery.parseResponse(SnmpDiscovery.java:78)
01-17 16:18:23.159: E/SnmpDiscovery(20142):     at com.hp.mobileprint.discoveryservice.MultiProtocolDiscovery.parseResponse(MultiProtocolDiscovery.java:91)
01-17 16:18:23.159: E/SnmpDiscovery(20142):     at com.hp.mobileprint.printservice.tasks.LocalPrinterDiscoveryTask.processIncomingPacket(LocalPrinterDiscoveryTask.java:177)
01-17 16:18:23.159: E/SnmpDiscovery(20142):     at com.hp.mobileprint.printservice.tasks.LocalPrinterDiscoveryTask.receiveResponsePackets(LocalPrinterDiscoveryTask.java:136)
01-17 16:18:23.159: E/SnmpDiscovery(20142):     at com.hp.mobileprint.printservice.tasks.LocalPrinterDiscoveryTask.doInBackground(LocalPrinterDiscoveryTask.java:77)
01-17 16:18:23.159: E/SnmpDiscovery(20142):     at com.hp.mobileprint.printservice.tasks.LocalPrinterDiscoveryTask.doInBackground(LocalPrinterDiscoveryTask.java:51)
01-17 16:18:23.159: E/SnmpDiscovery(20142):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-17 16:18:23.159: E/SnmpDiscovery(20142):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-17 16:18:23.159: E/SnmpDiscovery(20142):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-17 16:18:23.159: E/SnmpDiscovery(20142):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-17 16:18:23.159: E/SnmpDiscovery(20142):     at java.lang.Thread.run(Thread.java:841)
01-17 16:18:23.159: W/PRINT_LIB(20142): Printer could not be parsed or is not supported.
01-17 16:18:23.159: D/PRINT_LIB(20142): Response from /192.168.1.1:161
01-17 16:18:23.169: E/SnmpDiscovery(20142): Error while reading SNMP response.
01-17 16:18:23.169: E/SnmpDiscovery(20142): java.net.ProtocolException: SNMP error: 2
01-17 16:18:23.169: E/SnmpDiscovery(20142):     at com.hp.mobileprint.discoveryservice.parsers.DeviceStatus.<init>(DeviceStatus.java:117)
01-17 16:18:23.169: E/SnmpDiscovery(20142):     at com.hp.mobileprint.discoveryservice.SnmpDiscovery.parseResponse(SnmpDiscovery.java:78)
01-17 16:18:23.169: E/SnmpDiscovery(20142):     at com.hp.mobileprint.discoveryservice.MultiProtocolDiscovery.parseResponse(MultiProtocolDiscovery.java:91)
01-17 16:18:23.169: E/SnmpDiscovery(20142):     at com.hp.mobileprint.printservice.tasks.LocalPrinterDiscoveryTask.processIncomingPacket(LocalPrinterDiscoveryTask.java:177)
01-17 16:18:23.169: E/SnmpDiscovery(20142):     at com.hp.mobileprint.printservice.tasks.LocalPrinterDiscoveryTask.receiveResponsePackets(LocalPrinterDiscoveryTask.java:136)
01-17 16:18:23.169: E/SnmpDiscovery(20142):     at com.hp.mobileprint.printservice.tasks.LocalPrinterDiscoveryTask.doInBackground(LocalPrinterDiscoveryTask.java:77)
01-17 16:18:23.169: E/SnmpDiscovery(20142):     at com.hp.mobileprint.printservice.tasks.LocalPrinterDiscoveryTask.doInBackground(LocalPrinterDiscoveryTask.java:51)
01-17 16:18:23.169: E/SnmpDiscovery(20142):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
01-17 16:18:23.169: E/SnmpDiscovery(20142):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
01-17 16:18:23.169: E/SnmpDiscovery(20142):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-17 16:18:23.169: E/SnmpDiscovery(20142):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-17 16:18:23.169: E/SnmpDiscovery(20142):     at java.lang.Thread.run(Thread.java:841)
01-17 16:18:23.169: W/PRINT_LIB(20142): Printer could not be parsed or is not supported.
01-17 16:18:23.169: D/PRINT_LIB(20142): Response from /192.168.1.35:5353
01-17 16:18:23.169: D/BonjourParser(20142): Found PDL service without support for HP PCL: Brother QL-710W._pdl-datastream._tcp.local
4

0 回答 0