我已經關注了這個PrintService示例存盤庫中的很多代碼:Zaki50 PrintService。我想要做的唯一不同的事情是從與PrintJob. 現在我有了FileDescriptor,但不知道如何使用它來獲取實際的檔案資料!任何幫助將不勝感激。
uj5u.com熱心網友回復:
好的,這是代碼中的答案:
final PrintJobInfo info = printJob.getInfo();
final File file = new File(getFilesDir(), info.getLabel());
InputStream in = null;
FileOutputStream out = null;
try {
in = new
FileInputStream(printJob.getDocument().getData().getFileDescriptor());
out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
out.flush();
out.close();
} catch (IOException ioe) {
}
FileChannel channel = null;
byte[] fileBytes = null;
try {
RandomAccessFile raf = new RandomAccessFile(file, "r");
channel = raf.getChannel();
ByteBuffer bb = ByteBuffer.NEW(channel.map(
FileChannel.MapMode.READ_ONLY, 0, channel.size()));
fileBytes = new byte[bb.remaining()];
bb.get(fileBytes , 0, fileBytes .length);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
陣列的內容fileBytes是列印檔案的實際位元組。
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/483062.html
下一篇:沒有匯入處理檔案的時間很長
