首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用Android PdfViewer编写jetpack

如何使用Android PdfViewer编写jetpack
EN

Stack Overflow用户
提问于 2022-03-19 12:20:08
回答 1查看 797关注 0票数 -1

在对"Android PdfViewer“进行了一些研究之后,我想在我的应用程序中打开pdf,但是我不知道如何使用它,jetpack使用互操作性,因为它是java库。

这是我如何使用安卓PdfViewer,但它不是渲染的pdf。

代码语言:javascript
复制
@Composable

fun PdfViewer(
modifier: Modifier = Modifier,
dashboardViewModel: DashboardViewModel
) {
AndroidView(
    modifier = modifier
        .fillMaxSize()
        .padding(top = 4.dp, bottom = 24.dp),
    factory = { context ->
        PDFView(context = context, set = null).apply {
            Timber.i(File(dashboardViewModel.pdfUri.value).toUri().toString())
            fromUri(File(dashboardViewModel.pdfUri.value).toUri())
                .load()
        }
    })
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-22 04:51:36

它不起作用,所以我做了一个新的活动,在其中我使用xml而不是可组合的。

为了渲染pdf,我正在使用Android PdfViewer库。

PdfActivity.kt

代码语言:javascript
复制
class PdfActivity : AppCompatActivity() {

private lateinit var fileName: String
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_pdf)

    val pdfLayout = findViewById<PDFView>(R.id.pdfViewLayout)
    val intent = intent

    if (intent.extras != null) {
        fileName = intent.extras?.getString("fileName").toString()
    }

    try {
        pdfLayout.fromUri(File("${applicationContext.getExternalFilesDir(null)}/$fileName.pdf").toUri())
            .autoSpacing(true)
            .fitEachPage(true)
            .load()
    } catch (e: Exception) {
        Timber.d(e.message)
    }
}
}

activity_pdf.xml

代码语言:javascript
复制
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".PdfActivity">

<com.github.barteksc.pdfviewer.PDFView
    android:id="@+id/pdfViewLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71538086

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档