[英]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.