gpt4 book ai didi

Kotlin 下载并显示 PDF

转载 作者:行者123 更新时间:2023-12-02 13:20:19 24 4
gpt4 key购买 nike

嗨,我想使用 Kotlin 下载并显示 pdf。我想下载并在我们下载的同时显示pdf显示的第一页的进度条。然后显示PDF。我用 PDFKit 快速完成了它,它非常简单,但我在 Kotlin 中找不到等价物。

我做了很多研究以在 Kotlin 中显示 pdf,但我没有得到太多结果,似乎这个主题并不是那么首先我查看了原生的 PdfRenderer,但大多数示例都是在 java 中而不是 Kotlin 中,我没有得到什么是:documented.getSeekableFileDescriptor()。

然后我查看了 pdfView lib,它非常适合显示 pdf,但仅来自 Assets ,pdfView.fromStream 似乎不起作用,我无法获得任何工作示例。更多我想避免下载pdf 我想直接显示它以避免长时间加载。

最后,我使用 okhttp 和改造来下载 pdf,但我不能使用 pdfView 来显示它,因为在来自 Assets 的 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()
})
}

最佳答案

如果有人遇到这个问题,我使用 coroutine 解决了它:
将此依赖项添加到您的 Gradle 构建中: implementation("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.3.2")

然后 import : import 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 因为你可以在 asyctask 中使用全局 View 。

现在要添加一个进度条,您可以添加到您的 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 以获取您的progressera 的回调并为您的事件实现 OnLoadCompleteListener 接口(interface)。

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

关于Kotlin 下载并显示 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58233605/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com