在对"Android PdfViewer“进行了一些研究之后,我想在我的应用程序中打开pdf,但是我不知道如何使用它,jetpack使用互操作性,因为它是java库。
这是我如何使用安卓PdfViewer,但它不是渲染的pdf。
@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()
}
})
}发布于 2022-03-22 04:51:36
它不起作用,所以我做了一个新的活动,在其中我使用xml而不是可组合的。
为了渲染pdf,我正在使用Android PdfViewer库。
PdfActivity.kt
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
<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>https://stackoverflow.com/questions/71538086
复制相似问题