You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

CameraX PreviewView显示黑屏问题排查与解决求助

CameraX预览黑屏问题排查与解决

一、权限相关说明与修复

  • 无需RECORD_AUDIO权限:仅实现相机预览功能不需要该权限,直接从Manifest中移除即可。
  • 必须完成动态权限申请:CAMERA属于危险权限,仅在Manifest中声明静态权限无效,Android 6.0(API 23)及以上系统必须弹窗向用户申请授权,未授权时相机无法正常访问,这是黑屏最常见的诱因。

二、代码调整建议

针对你提供的代码,做以下几点修改:

  1. 切换PreviewView实现模式:将ImplementationMode.COMPATIBLE改为ImplementationMode.PERFORMANCE,该模式优先使用硬件加速的SurfaceView,性能和兼容性更优,可解决部分设备的黑屏问题。
  2. 完善错误日志输出:替换e.printStackTrace()为详细日志打印,方便定位具体报错原因(如权限拒绝、相机资源占用等)。
  3. 集成动态权限申请逻辑:在Composable中加入权限判断流程,确保获得相机权限后再加载预览。

修改后的完整代码示例:

@Composable
fun MyCameraPreview() {
    val context = LocalContext.current
    val lifecycleOwner = LocalLifecycleOwner.current
    val cameraPermissionState = rememberPermissionState(android.Manifest.permission.CAMERA)

    // 启动时检查权限,未授权则弹窗申请
    LaunchedEffect(Unit) {
        if (!cameraPermissionState.hasPermission) {
            cameraPermissionState.launchPermissionRequest()
        }
    }

    // 权限通过才渲染预览组件
    if (cameraPermissionState.hasPermission) {
        AndroidView(factory = { ctx ->
            val previewView = PreviewView(ctx).apply {
                scaleType = PreviewView.ScaleType.FILL_CENTER
                layoutParams = ViewGroup.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT,
                    ViewGroup.LayoutParams.MATCH_PARENT
                )
                implementationMode = PreviewView.ImplementationMode.PERFORMANCE
            }

            ProcessCameraProvider.getInstance(ctx).apply {
                addListener({
                    val cameraProvider = this.get()
                    val preview = androidx.camera.core.Preview.Builder().build().apply {
                        setSurfaceProvider(previewView.surfaceProvider)
                    }

                    try {
                        cameraProvider.unbindAll()
                        cameraProvider.bindToLifecycle(
                            lifecycleOwner,
                            CameraSelector.DEFAULT_BACK_CAMERA,
                            preview
                        )
                    } catch (e: Exception) {
                        Log.e("CameraPreview", "相机绑定生命周期失败", e)
                    }
                }, ContextCompat.getMainExecutor(ctx))
            }

            previewView
        })
    } else {
        // 权限未授予时显示提示UI
        Text(text = "需要相机权限才能显示预览")
    }
}

三、日志错误「尝试重新打开相机10000ms未成功」的解决

该错误的常见触发原因及解决方式:

  • 权限未授予:动态权限申请未通过,导致相机初始化失败,系统持续重试。确保完成权限申请流程即可。
  • 相机资源被占用:其他应用(如系统相机、第三方扫码工具)正在占用相机,重启设备可释放资源。
  • 设备兼容性问题:部分设备需指定预览分辨率,可在Preview.Builder中添加配置:
    val preview = androidx.camera.core.Preview.Builder()
        .setTargetResolution(Size(1920, 1080)) // 选择设备支持的分辨率
        .build()
    

额外检查项

  • 打开系统相机确认硬件功能正常;
  • 检查CameraX依赖版本是否为最新稳定版,版本不兼容也可能引发异常。

内容的提问来源于stack exchange,提问作者user1785730

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.06 21:24:51