使用Android打印PDF

时间:2013-04-19 09:32:20

标签: android pdf printing

我正在尝试从我的Android应用程序中打印PDF。但每次我尝试打印我的打印页面都包含奇怪的数据,如:

  java.io.FileInputStream@418479b0

我认为我的pdf没有以正确的方式呈现...... 现在有人如何正确地将我的pdf转换为我的输出流?

我已经尝试过这个问题的代码(Printing pdf in android),但后来我在打印页面上得到了以下输出:

   Filter/FlateDecode/Length 69 >>stream

任何人都可以帮助我吗? :)

这是我的代码:

    Socket socket = new Socket();
    String sIP = "192.168.0.250";
    String sPort = "9100";

    InetSocketAddress socketAddress = new InetSocketAddress(sIP, Integer.parseInt(sPort));
    DataOutputStream outputStream;

    try{
        //file init
        File pdfFile = new File(file);
        byte[] byteArray=null;
        InputStream inputStream = new FileInputStream(pdfFile);
        String inputStreamToString = inputStream.toString();
        byteArray = inputStreamToString.getBytes();
        inputStream.close();

        //Socket init
        socket.connect(socketAddress, 3000);
        outputStream = new DataOutputStream(socket.getOutputStream());
        outputStream.write(byteArray);
        outputStream.close();
        socket.close();
    }catch(Exception e){
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

您可以尝试这样的方法来获取byteArray:

//file init
File pdfFile = new File(file);
byte[] byteArray = new byte[(int) pdfFile.length()];
FileInputStream fis = new FileInputStream(pdfFile);
fis.read(byteArray);
fis.close();