안드로이드 어플리케이션 개발중 프린트 출력하기 기능을 알아보았다.
크게 2가지로 나뉘는데
1. 구글 클라우드 프린트이용
2. 안드로이드자체제 공해주는 printhelper 를 이용하는 것이다.
오늘은 후자에 대해 메모를 하려고 한다.
나는 pdf 파일을 출력하는 방식으로 코딩을 하였다.
public void doPhotoPrint(String filePath){
PrintManager printManager = null;
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.KITKAT) {
printManager = (PrintManager) this.getSystemService(Context.PRINT_SERVICE);
String jobName = this.getString(R.string.app_name) + " Document";
printManager.print(jobName, pda, null);
}else{
Toast.makeText(act, "안드로이드 4.4버전 이상으로만 사용가능합니다.", Toast.LENGTH_SHORT).show();
}
}
PrintDocumentAdapter pda = new PrintDocumentAdapter()
{
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onWrite(PageRange[] pages, ParcelFileDescriptor destination, CancellationSignal cancellationSignal, WriteResultCallback callback)
{
InputStream input = null;
OutputStream output = null;
try {
input = new FileInputStream(new File("/storage/emulated/0/Panda/파일경로"));
output = new FileOutputStream(destination.getFileDescriptor());
byte[] buf = new byte[1024];
int bytesRead;
while ((bytesRead = input.read(buf)) > 0) {
output.write(buf, 0, bytesRead);
}
}
catch (Exception e) {
} finally {
try {
input.close();
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
callback.onWriteFinished(new PageRange[]{PageRange.ALL_PAGES});
}
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onLayout(PrintAttributes oldAttributes, PrintAttributes newAttributes, CancellationSignal cancellationSignal, LayoutResultCallback callback, Bundle extras)
{
if (cancellationSignal.isCanceled())
{
callback.onLayoutCancelled();
return;
}
//int pages = computePageCount(newAttributes);
PrintDocumentInfo pdi = new PrintDocumentInfo.Builder("file_name.pdf").setContentType(PrintDocumentInfo.CONTENT_TYPE_DOCUMENT).build();
callback.onLayoutFinished(pdi, true);
}
};
더 좋은 아이디어가 있으시면 언제든지 답글을 달아주세요~!
'IT개발일지 > Android' 카테고리의 다른 글
[Android] PDF 파일 합치기 merge (0) | 2019.01.21 |
---|---|
[Android] 안드로이드 PDF 파일 열기 (0) | 2019.01.21 |
[Android] Glide 이용하여 RGB 색상채우기 (0) | 2018.12.12 |
[Android] Broadcast 휴대폰 부팅 프로세스 (0) | 2018.12.11 |
[Android] notification 중복 알림 방지 (0) | 2018.11.07 |