简体   繁体   中英

Cannot print pdf format in android

When I print pdf file by using the following code in android, print output is not correctly,

Here is my code ,

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;
    }
}

Text format(.txt) is OK.This is sample output for pdf,

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

or something like that. How can I solve it.

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
}   

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM