[英]PrintedPdfDocument bmp to pdf
我正在尝试将 bmp 图像保存到 pdf:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
try {
scannedImageView.setImageBitmap(bitmap);
// open a new document
PrintedPdfDocument document = new PrintedPdfDocument(context, bitmap); // cannot resolve symbol context.
// start a page
PdfDocument.Page page = document.startPage(0);
// draw something on the page
View content = scannedImageView;
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
// add more pages
// write the document content
document.writeTo(getOutputStream()); // cannot resolve method getOutputStream()
//close the document
document.close();
}
我在context
和getOutputStream()
中评论了错误。 任何想法如何解决?
Activity
class 您应该将context
替换为this
。Fragment
class ,则应将context
替换为getContext()
。androidx.fragment.app.Fragment
您应该能够使用requireContext()
因为它确保您将收到Context
object 否则它将引发异常。 以下是 androidx.fragment.app.Fragment 中getContext()
和requireContext()
方法的androidx.fragment.app.Fragment
:
getOutputStream()
未在Activity
或Fragment
中定义。 This is the function you should define that will return output stream that will define to which file your PDF will be written to. 您可能想看看FileOutputStream 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.