当我在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
......
或类似的东西。我该怎么解决呢。
答案 0 :(得分:-1)
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
}