PrintDocumentAdapter pda = new PrintDocumentAdapter() {
@Override
public void onWrite(PageRange[] pages, final ParcelFileDescriptor destination, CancellationSignal cancellationSignal, final WriteResultCallback callback) {
Log.i("Write", "I Visited in Write");
AsyncTask.execute(new Runnable() {
@Override
public void run() {
InputStream input = null;
OutputStream output = null;
try {
String myUrlStr = Constant.URL_FILE + publisherId + "/" + filePath;
URL aURL = new URL(myUrlStr);
URLConnection conn = aURL.openConnection();
conn.connect();
input = conn.getInputStream();
output = new FileOutputStream(destination.getFileDescriptor());
byte[] buf = new byte[1024];
/* byte[] key = generateKey(password);
byte[] decode = decodeFile(key, buf);*/
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
} catch (FileNotFoundException ee) {
ee.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (input != null)
input.close();
if (output != null)
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
});
}
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras) {
if (cancellationSignal.isCanceled()) {
callback.onLayoutCancelled();
return;
}
PrintDocumentInfo pdi = new PrintDocumentInfo.Builder(getString(R.string.app_name) + " - " + filePath).setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
callback.onLayoutFinished(pdi, true);
}
};
return pda;
}
PrintManager printManager = (PrintManager) PreviewPdfActivity.this.getSystemService(Context.PRINT_SERVICE);
String jobName = PreviewPdfActivity.this.getString(R.string.app_name) + " Document";
printManager.print(jobName, pda, null);
С помощью приведенного выше кода я могу правильно распечатать PDF-файл с URL-адреса сервера, но если PDF-файл с URL-адреса сервера защищен паролем, Logcat отображает ошибку не может распечатать PDF-файл с защитой паролем.Так может кто-нибудь, пожалуйста, помогите мне найти решение.Я много гуглил для решения, но не нашел подходящего ответа.