簡體   English   中英

無法在Android中打印pdf格式

[英]Cannot print pdf format in android

當我在android中使用以下代碼打印pdf文件時,打印輸出不正確,

這是我的代碼,

private class PrintFile extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... result) {

           FileInputStream fileInputStream;
           BufferedInputStream bufferedInputStream = null;

        File file = new File(DB_PATH);

         try {
         Socket client = new Socket("172.230.1.16", 9100);

         byte[] mybytearray = new byte[(int) file.length()];

         fileInputStream = new FileInputStream(file);
         bufferedInputStream = new BufferedInputStream(fileInputStream);

         bufferedInputStream.read(mybytearray, 0, mybytearray.length);
         outputStream = client.getOutputStream();

         outputStream.write(mybytearray, 0, mybytearray.length); //
         outputStream.flush();
         bufferedInputStream.close();
         outputStream.close();
         client.close();

         } catch (UnknownHostException e) {
         e.printStackTrace();
         } catch (IOException e) {
         e.printStackTrace();
         }
        return null;
    }
}

文本格式(.txt)正常。這是pdf的示例輸出,

1 0 obj
       << /Creater (Prawn)
                         /Producre (Prawn)
                                          >> endobj
                                                   2 0 obj
                                                          ......

或類似的東西。 我該怎么解決。

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(Uri.fromFile(file),"application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(target, "Open File");
try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    // Instruct the user to install a PDF reader here, or something
}   

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM