1

我正在使用 android pdfviewer lib 打开和阅读 pdf,位于:https ://github.com/barteksc/AndroidPdfViewer

但是当我尝试启动 pdf 时出现错误:

E/zygote64:未找到长 com.shockwave.pdfium.PdfiumCore.nativeOpenDocument(int, java.lang.String) 的实现(已尝试 Java_com_shockwave_pdfium_PdfiumCore_nativeOpenDocument 和 Java_com_shockwave_pdfium_PdfiumCore_nativeOpenDocument__ILjava_lang_String_2)

E/PDFView: 加载 pdf 错误 java.lang.UnsatisfiedLinkError: No implementation found for long com.shockwave.pdfium.PdfiumCore.nativeOpenDocument(int, java.lang.String) (试过 Java_com_shockwave_pdfium_PdfiumCore_nativeOpenDocument 和 Java_com_shockwave_pdfium_PdfiumCore_nativeOpenDocument__ILjava_lang_String_2)

我尝试了依赖项的不同实现,但没有一个起作用:

implementation 'com.github.barteksc:pdfium-android:1.9.0'
implementation "com.github.barteksc:android-pdf-viewer:3.2.0-beta.1"
implementation "com.github.barteksc:android-pdf-viewer:2.8.2"

错误在这里找到:

public PdfDocument newDocument(ParcelFileDescriptor fd, String password) throws IOException {
        PdfDocument document = new PdfDocument();
        document.parcelFileDescriptor = fd;
        synchronized (lock) {
            document.mNativeDocPtr = nativeOpenDocument(getNumFd(fd), password);
        }

        return document;
    }

库中的函数 nativeOpenDocument 似乎没有加载。

我在 github 上发现了一些话题:https ://github.com/barteksc/AndroidPdfViewer/issues/538 https://github.com/barteksc/PdfiumAndroid/issues/54 https://github.com/mshockwave/ PdfiumAndroid/问题/13

但是没有找到解决方案,正如建议的那样,我尝试更改依赖项,尝试关闭计算机和手机,尝试使缓存无效并重新启动,在模拟器上尝试但没有任何效果。

如果有人可以帮助我,那就太好了?

4

2 回答 2

2

我刚刚发现,在我的情况下,我有一个应用程序和一个模块,我自己的库 aar 是我在其中使用 androidpdfviewer 的地方。

问题是我只依赖于库而不是应用程序。

为了解决这个问题,我必须在应用程序和库中添加依赖项。现在我看到我有 2 个 build.gradle 似乎很明显。现在它工作正常。

添加classpath 'com.github.barteksc:pdfium-android:1.9.0'你的 build.gradle 但项目不是模块。

于 2020-07-16T14:39:57.033 回答
1

这就是我加载pdf url的方式:

fragment_pdf_reader.xml:

<androidx.constraintlayout.widget.ConstraintLayout
//....
<com.github.barteksc.pdfviewer.PDFView
            android:id="@+id/pdfView"
            android:layout_width="match_parent"
            android:layout_height="@dimen/zeroDp"
            android:visibility="visible"
            app:layout_constraintBottom_toTopOf="@+id/tvFeedback"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/view" />
//...
</androidx.constraintlayout.widget.ConstraintLayout>

现在在您的片段中获取 pdf url 并使用该 pdf url 发出 Http 请求并获取 ResponseBody 将其传递给以下方法:

private fun downloadFile(responseBody: ResponseBody) {// get responseBody after making Http req . I'm using retrofit 
        binding?.let {
            //it.pdfView.removeAllViews()
            it.pdfView.fromBytes(responseBody.bytes())
                    .defaultPage(pageNumber)
                    .onPageChange(this@PdfReaderFragment)
                    .enableAnnotationRendering(true)
                    .onLoad(this@PdfReaderFragment)
                    .spacing(10) // in dp
                    .onPageError(this@PdfReaderFragment)
                    .onError { onError("File not in PDF format or corrupted") }
                    .load()
        }


    }

实现这些回调:

OnPageChangeListener
OnLoadCompleteListener
OnPageErrorListener

上述回调的实现是:

override fun loadComplete(nbPages: Int) {
        binding?.let {
            printBookmarksTree(it.pdfView.tableOfContents, "-")
        }

    }

override fun onPageError(page: Int, t: Throwable?) {
        Log.d("PDF", t?.message)
    }
override fun onPageChanged(page: Int, pageCount: Int) {
        pageNumber = page
        Log.d("PDF", "Page number $pageNumber $pageCount")
        setTitleMessageBackArrow(String.format("%s %s / %s", "your fragment title", page + 1, pageCount))
    }

    private fun printBookmarksTree(tree: List<Bookmark>, sep: String) {
        for (b in tree) {
            Log.e("PDF", String.format("%s %s, p %d", sep, b.title, b.pageIdx))
            if (b.hasChildren()) {
                printBookmarksTree(b.children, "$sep-")
            }
        }
    }
于 2020-07-16T13:34:06.577 回答