Kotlin下载并显示PDF

时间:2019-10-04 09:29:44

标签: kotlin retrofit2 okhttp pdfrenderer

嗨,我想使用Kotlin下载并显示pdf。我想在下载时下载并在pdf显示的第一页上显示进度条,然后显示PDF。我使用PDFKit迅速完成了它,这非常简单,但是我在Kotlin中找不到等效项。

我进行了很多研究,以在Kotlin中显示pdf,但是我没有得到太多的结果,似乎这个主题并不是真的,所以我首先看了原生的PdfRenderer,但是大多数示例使用的是Java,而不是Kotlin,而我不知道是什么:documented.getSeekableFileDescriptor()。

然后我查看了pdfView库,它非常适合显示pdf,但仅显示资产中的pdfView.fromStream似乎不起作用,我无法举例说明它的工作方式。为了避免下载pdf,我想直接显示它,以避免加载时间过长。

最后,我使用okhttp进行了改造并下载了pdf,但是我不能使用pdfView来显示它,因为在from资产中pdf必须已经在项目中。

我发现从和从url下载pdf并用Kotlin进行显示非常困难,而且文档也很少。

因此,如果有人有任何建议,我会接受。

这是我使用pdfView.fromStream的代码示例,只会加载空白页面

 private fun loadpdf(){
        println("pdfview")
        //PDF View
        Thread(Runnable {
            val input = URL(pdf_url).openStream()
            val pdfView = this.findViewById<PDFView>(com.example.mylibrary.R.id.pdfView)
            //pdfView.fromFile(file)
            pdfView.fromStream(input)
                .enableSwipe(true) // allows to block changing pages using swipe
                .swipeHorizontal(true)
                .enableDoubletap(true)
                .defaultPage(0)
                .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
                .password(null)
                .scrollHandle(null)
                .enableAntialiasing(true) // improve rendering a little bit on low-res screens
                // spacing between pages in dp. To define spacing color, set view background
                .spacing(0)
                .pageFitPolicy(FitPolicy.WIDTH)
                .load()
            println("testpdf")
        })
    }

这是我使用pdfView.fromAsset的代码示例,这项工作有效,但前提是文件已经在项目中,但是我想从url获取我的pdf

private fun loadpdf(){
        //PDF View
        Thread(Runnable {
            val pdfView = this.findViewById<PDFView>(com.example.mylibrary.R.id.pdfView)
            pdfView.fromAsset("url")
                .enableSwipe(true) // allows to block changing pages using swipe
                .swipeHorizontal(true)
                .enableDoubletap(true)
                .defaultPage(0)
                .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
                .password(null)
                .scrollHandle(null)
                .enableAntialiasing(true) // improve rendering a little bit on low-res screens
                // spacing between pages in dp. To define spacing color, set view background
                .spacing(0)
                .pageFitPolicy(FitPolicy.WIDTH)
                .load()
        })
    }

1 个答案:

答案 0 :(得分:1)

如果有人遇到这个问题,我可以使用协程解决它: 将此依赖项添加到您的Gradle构建中:实现(“ org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2”)

然后导入:将kotlinx.coroutines。*导入文件中。

然后使用lauch协程方法如下:

private fun loadpdf(pdfView: PDFView){
        //PDF View
        GlobalScope.launch{
            val input : InputStream
            input = URL(pdf_url).openStream()
            pdfView.fromStream(input)
                .enableSwipe(true) // allows to block changing pages using swipe
                .swipeHorizontal(true)
                .enableDoubletap(true)
                .defaultPage(0)
                .enableAnnotationRendering(false) // render annotations (such as comments, colors or forms)
                .password(null)
                .scrollHandle(null)
                .enableAntialiasing(true) // improve rendering a little bit on low-res screens
                // spacing between pages in dp. To define spacing color, set view background
                .spacing(0)
                .pageFitPolicy(FitPolicy.WIDTH)
                .load()
        }

您必须传递pdfView cos,才能在asyctask中使用全局视图。

现在要添加进度条,您可以将其添加到xml中:

<ProgressBar
            android:id="@+id/Progressbar"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerInParent="true"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:layout_gravity="center"
            android:indeterminate="true"
            android:minWidth="200dp"
            android:minHeight="50dp" />

然后,您需要重写loadComplete函数以获取进度条的回调,并为您的活动实现OnLoadCompleteListener接口。

和顺便说一句,如果要使用pdfView lib显示每页的页面,请添加: .swipeHorizo​​ntal(true) .pageSnap(true) .autoSpacing(true) .pageFling(true)