繁体   English   中英

PrintedPdfDocument bmp to pdf

[英]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();
}

我在contextgetOutputStream()中评论了错误。 任何想法如何解决?

  • 如果您正在使用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()未在ActivityFragment中定义。 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM