kotlin 使用pspdfkit在Jetpack compose中打开Pdf

5vf7fwbs  于 2023-05-18  发布在  Kotlin
关注(0)|答案(1)|浏览(161)

我正在使用KotlinJetpack,打开文档时出现以下错误。

com.pspdfkit.jetpack.compose.NonFragmentActivityException: You need to use a fragment activity in order to use the DocumentView composable.
        at com.pspdfkit.jetpack.compose.DocumentViewKt.DocumentView(SourceFile:228)
        at com.harry.pennysaver.views.pdf.ReaderKt$PdfList$1$5.invoke(Reader.kt:142)
        at com.harry.pennysaver.views.pdf.ReaderKt$PdfList$1$5.invoke(Reader.kt:124)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:116)
        at androidx.compose.runtime.internal.ComposableLambdaImpl.invoke(ComposableLambda.jvm.kt:34)
        at androidx.compose.animation.AnimatedVisibilityKt.AnimatedEnterExitImpl(AnimatedVisibility.kt:935)
        at androidx.compose.animation.AnimatedVisibilityKt.AnimatedVisibility(AnimatedVisibility.kt:128)
        at com.harry.pennysaver.views.pdf.ReaderKt.PdfList(Reader.kt:120)
        at com.harry.pennysaver.views.home.HomeScreenKt.HomeScreen(HomeScreen.kt:47)
        at com.harry.pennysaver.views.home.HomeScreenKt$HomeScreen$2.invoke(Unknown Source:10)
        at com.harry.pennysaver.views.home.HomeScreenKt$HomeScreen$2.invoke(Unknown Source:10)
        at androidx.compose.runtime.RecomposeScopeImpl.compose(RecomposeScopeImpl.kt:145)
        at androidx.compose.runtime.ComposerImpl.recomposeToGroupEnd(Composer.kt:2375)
        at androidx.compose.runtime.ComposerImpl.skipCurrentGroup(Composer.kt:2643)
        at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:3260)
        at androidx.compose.runtime.ComposerImpl$doCompose$2$5.invoke(Composer.kt:3238)
        at androidx.compose.runtime.SnapshotStateKt__DerivedStateKt.observeDerivedStateRecalculations(DerivedState.kt:341)
        at androidx.compose.runtime.SnapshotStateKt.observeDerivedStateRecalculations(Unknown Source:1)
        at androidx.compose.runtime.ComposerImpl.doCompose(Composer.kt:3238)
        at androidx.compose.runtime.ComposerImpl.recompose$runtime_release(Composer.kt:3203)
        at androidx.compose.runtime.CompositionImpl.recompose(Composition.kt:771)
        at androidx.compose.runtime.Recomposer.performRecompose(Recomposer.kt:1031)
        at androidx.compose.runtime.Recomposer.access$performRecompose(Recomposer.kt:125)
        at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:534)
        at androidx.compose.runtime.Recomposer$runRecomposeAndApplyChanges$2$2.invoke(Recomposer.kt:503)
        at androidx.compose.ui.platform.AndroidUiFrameClock$withFrameNanos$2$callback$1.doFrame(AndroidUiFrameClock.android.kt:34)
        at androidx.compose.ui.platform.AndroidUiDispatcher.performFrameDispatch(AndroidUiDispatcher.android.kt:109)
        at androidx.compose.ui.platform.AndroidUiDispatcher.access$performFrameDispatch(AndroidUiDispatcher.android.kt:41)
        at androidx.compose.ui.platform.AndroidUiDispatcher$dispatchCallback$1.doFrame(AndroidUiDispatcher.android.kt:69)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:970)
        at android.view.Choreographer.doCallbacks(Choreographer.java:796)
        at android.view.Choreographer.doFrame(Choreographer.java:727)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:957)
        at android.os.Handler.handleCallback(Handler.java:938)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
        Suppressed: kotlinx.coroutines.DiagnosticCoroutineContextException: [androidx.compose.runtime.PausableMonotonicFrameClock@4ce413b, StandaloneCoroutine{Cancelling}@253f058, AndroidUiDispatcher@d0855b1]

我的Pdf合成如下

private const val thumbnailPageIndex = 0

    @SuppressLint("pspdfkit-experimental")
    @OptIn(ExperimentalFoundationApi::class, ExperimentalPSPDFKitApi::class)
    @Composable
    fun PdfList(
        pdfState: PdfState,
        loadPdfs: () -> Unit,
        openDocument: (Uri) -> Unit
    ) {
        Box(
            contentAlignment = Alignment.Center,
            modifier = Modifier.fillMaxSize()
        ) {
            AnimatedVisibility(
                visible = pdfState.loading,
                enter = fadeIn(),
                exit = fadeOut()
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.padding(24.dp)
                ) {
                    Text(
                        text = "Loading....",
                        textAlign = TextAlign.Center
                    )
                    VerticalSpacer(height = 8.dp)
                    CircularProgressIndicator()
                }
    
            }
    
            AnimatedVisibility(
                visible = !pdfState.loading && pdfState.documents.isEmpty(),
                enter = fadeIn(),
                exit = fadeOut()
            ) {
                Column(
                    horizontalAlignment = Alignment.CenterHorizontally,
                    modifier = Modifier.padding(24.dp)
                ) {
                    Text(
                        text = "No documents yet",
                        textAlign = TextAlign.Center
                    )
                    VerticalSpacer(height = 8.dp)
    
                    PrimaryButton(
                        label = "Load Documents",
                        modifier = Modifier
                            .height(44.dp)
                            .fillMaxSize()
                            .padding(horizontal = 55.dp)
                    ) {
                        loadPdfs()
                    }
                }
            }
    
            AnimatedVisibility(
                visible = !pdfState.loading && pdfState.documents.isNotEmpty(),
                enter = fadeIn(),
                exit = fadeOut()
            ) {
                LazyVerticalGrid(
                    cells = GridCells.Adaptive(100.dp),
                    modifier = Modifier.fillMaxSize()
                ) {
                    items(pdfState.documents.toList()) {(uri, document) ->
                        PdfThumbnail(
                            document = document,
                            onClick = { openDocument(uri) }
                        )
                    }
                }
            }
    
            AnimatedVisibility(
                visible = pdfState.selectedDocumentUri != null,
                enter = slideInHorizontally{ it },
                exit = slideOutHorizontally{ it }
            ) {
    
                if (pdfState.selectedDocumentUri == null) {
                    Box(modifier = Modifier.fillMaxSize())
                } else {
                    val context = LocalContext.current
                    val pdfActivityConfiguration = remember {
                        PdfActivityConfiguration
                            .Builder(context)
                            .setUserInterfaceViewMode(UserInterfaceViewMode.USER_INTERFACE_VIEW_MODE_HIDDEN)
                            .build()
                    }
    
                    val documentState = rememberDocumentState(
                        pdfState.selectedDocumentUri,
                        pdfActivityConfiguration
                    )
    
                    DocumentView(
                        documentState = documentState,
                        modifier = Modifier.fillMaxSize()
                    )
                }
            }
        }
    }
    
    @Composable
    fun PdfThumbnail(
        document: PdfDocument,
        onClick: () -> Unit
    ) {
        val context = LocalContext.current
    
        val thumbnailImage = remember(document) {
            val pageImageSize = document.getPageSize(thumbnailPageIndex).toRect()
    
            document.renderPageToBitmap(
                context,
                thumbnailPageIndex,
                pageImageSize.width().toInt(),
                pageImageSize.height().toInt()
            ).asImageBitmap()
        }
    
        Card(
            elevation = 4.dp,
            modifier = Modifier
                .padding(8.dp)
                .clickable(
                    interactionSource = remember {
                        MutableInteractionSource()
                    },
                    indication = rememberRipple()
                ) {
                    onClick()
                }
        ) {
            Column {
                Image(
                    bitmap = thumbnailImage ,
                    contentDescription =  "Preview for document ${document.title}",
                    contentScale = ContentScale.Crop,
                    modifier = Modifier
                        .height(120.dp)
                        .fillMaxWidth()
                )
    
                VerticalSpacer(height = 8.dp)
    
                Text(
                    text = document.title ?: "Untitled Document",
                    modifier = Modifier.padding(12.dp),
                    style = TextStyle(
                        fontWeight = Typography.body1.fontWeight,
                        fontSize = Typography.body1.fontSize
                    )
                )
            }
        }
    }

现在,这个PdfList组合在我的HomeScreen.kt组合中使用如下。

@Composable
fun HomeScreen(
    viewModel: PdfViewModel? = hiltViewModel(),
    navController: NavController
) {
    val spacing = MaterialTheme.spacing
    Column(
        modifier = Modifier
            .fillMaxWidth()
            .wrapContentHeight()
            .padding(spacing.medium)
            .padding(top = spacing.extraLarge),
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        viewModel?.let {
            val state by it.state.collectAsState(PdfState())
            PdfList(pdfState = state, loadPdfs = viewModel::loadPdfs , openDocument = viewModel::openDocument)
        }

    }
}

我做错了什么?为什么当我点击card打开文档时会出现此错误。

mzsu5hc0

mzsu5hc01#

我写了一封信来支持这个问题,他们写了Document View()依赖于FragmentActivity,所以这个函数不能从JC ComponentActivity运行,尽管它被标记为Composable。
该API处于测试模式,将无法正常工作。
PSPDFKIT现在在JC上不起作用
以下是支持者的实际回应:Response from support

相关问题