0

我正在尝试将 PDF 转换为图像位图。为此,我正在使用下面的代码。

pdfFile = Environment.getExternalStorageDirectory().toString() + "/" + "imagepdf.pdf"

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            try {
                Uri uri = MainActivity.getUriFromFile(PdfExtrectorActivity.this,new File(pdfFile));
                final ParcelFileDescriptor parcelFileDescriptor = getContentResolver().openFileDescriptor(
                        uri, "r");
                final FileDescriptor fileDescriptor = parcelFileDescriptor.getFileDescriptor();
                pdfRenderer = new PdfRenderer(parcelFileDescriptor);
            } catch (IOException e) {
                e.printStackTrace();
            }
            Bitmap bitmap;
            final int pageCount = pdfRenderer.getPageCount();
            for (int i = 0; i < pageCount; i++) {
                PdfRenderer.Page page = pdfRenderer.openPage(i);


                bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
                page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);



                // close the page
                page.close();
            }

            // close the renderer
            pdfRenderer.close();
        }

当我将ParcelFileDescriptor发送到 pdfRenderer 时,它会抛出异常

java.io.IOException: cannot create document. Error: 3

我从 GitHub 问题中找到了一些链接。 https://github.com/googlesamples/android-PdfRendererBasic/issues/15 但它不会帮助我。

接受任何帮助。谢谢你。

4

1 回答 1

0

我的问题通过这个解决方案解决了,从 SD 卡中的 PDF 转换图像位图。,

这对我来说很好用

  File file = new File(getCacheDir(), PDF_FILE_NAME);
        //if (!file.exists()) {
        // Since PdfRenderer cannot handle the compressed asset file directly, we copy it into
        // the cache directory.
        //InputStream asset = context.getAssets().open("imagepdf.pdf");
        FileInputStream fileInputStream = null;
        FileOutputStream output = null;
        try {
            fileInputStream = new FileInputStream( Environment.getExternalStorageDirectory().toString() +  "/imagepdf.pdf");
            output = new FileOutputStream(file);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        final byte[] buffer = new byte[1024];
        int size;
        while ((size = fileInputStream.read(buffer)) != -1) {
            output.write(buffer, 0, size);
        }
        fileInputStream.close();
        output.close();
        //}
        ParcelFileDescriptor mFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
        // This is the PdfRenderer we use to render the PDF.
        PdfRenderer renderer = null;
        if (mFileDescriptor != null) {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                renderer = new PdfRenderer(mFileDescriptor);
                Bitmap bitmap;
                final int pageCount = renderer.getPageCount();
                for (int i = 0; i < pageCount; i++) {
                    PdfRenderer.Page page = renderer.openPage(i);

                    // say we render for showing on the screen
                    bitmap = Bitmap.createBitmap(500, 500, Bitmap.Config.ARGB_8888);
                    page.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
                    myImageBitmap.add(bitmap);
                    // do stuff with the bitmap

                    // close the page
                    page.close();
                }

                // close the renderer
                renderer.close();
            }
        }
于 2018-08-22T04:50:27.117 回答