访问android相机的权限问题

发布于 2025-01-11 02:00:40 字数 4907 浏览 0 评论 0原文

我是这个 Android 世界的新手,我正在尝试构建一个应用程序来记录视频和 GPS 信息。

我在访问相机时遇到一些问题。没有错误,但似乎未获得许可。

Manifest:我认为有很多多余的行,但我尝试了所有的可能性。

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-feature android:name="android.hardware.camera.any" />
    <uses-permission android:name="android.hardware.camera.any" />
    <uses-feature android:name="android.hardware.CAMERA2" />
    <uses-permission android:name="android.permission.CAMERA2" />
    <uses-feature android:name="android.hardware.CAMERAX" />
    <uses-permission android:name="android.permission.CAMERAX" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

启动相机:

  @SuppressLint("RestrictedApi")
    private void cameraXStart(ProcessCameraProvider cameraProvider) {

        cameraProvider.unbindAll();
        CameraSelector cameraSelector = new CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                .build();
        Preview preview = new Preview.Builder()
                .build();
        preview.setSurfaceProvider(previewView.getSurfaceProvider());

        // Image capture use case
        imageCapture = new ImageCapture.Builder()
                .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
                .build();

        // Video capture use case
        videoCapture = new VideoCapture.Builder()
                .setVideoFrameRate(30)
                .build();

        //bind to lifecycle:
        cameraProvider.bindToLifecycle((LifecycleOwner) this, cameraSelector, preview, imageCapture, videoCapture);

    }

然后,我有这个允许相机使用:

ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA}, 100);

当我跑步时,我得到这个L

D/Camera2CameraImpl:{Camera@721ce9d[id=0]} 取消计划的重新打开:androidx.camera.camera2.internal.Camera2CameraImpl$StateCallback$ScheduledReopen@5296b52 D/Camera2CameraImpl: {Camera@721ce9d[id=0]} 打开相机。 D/Camera2CameraImpl: {Camera@721ce9d[id=0]} 正在转换相机内部状态:REOPENING -->开幕 D/CameraStateMachine:新的公共相机状态 CameraState{type=OPENING, error=null} 来自 OPENING 和 null D/UseCaseAttachState:所有用例:[androidx.camera.core.ImageCapture-f0c976a1-2b26-4655-88d6-6790eae98eb6206680605, androidx.camera.core.VideoCapture-c788dcee-3839-4ff7-ad80-908046a7a19e41671826,androidx.camera.core.Preview-45eb99ce-6730-444c-a6dc-e4baabab208b69765876]用于相机:0 D/Camera2CameraImpl: {Camera@721ce9d[id=0]} 由于 validateClientPermissionsLocked:913 无法打开相机:调用者“com.example.trucklogger”(PID 10105,UID 5516)在没有相机权限的情况下无法打开相机“0” D/Camera2CameraImpl: {Camera@721ce9d[id=0]} 转换相机内部状态:OPENING -->重新开放 D/CameraStateMachine:新的公共相机状态 CameraState{type=OPENING, error=null} 来自 OPENING 和 null D/Camera2CameraImpl: {Camera@721ce9d[id=0]} 尝试在 1000 毫秒内重新打开相机: androidx.camera.camera2.internal.Camera2CameraImpl$StateCallback$ScheduledReopen@a4eea9e activeResuming = true D/Camera2CameraImpl: {Camera@721ce9d[id=0]} 尝试强制打开相机。 D/CameraStateRegistry: tryOpenCamera(Camera@721ce9d[id=0]) [可用相机: 0, 已打开: true (先前状态: OPENING)] -->成功 D/CameraStateRegistry:重新计算打开的相机: 相机状态
-------------------------------------------------- ----------------- Camera@dd4530c[id=1] 未知
相机@721ce9d[id=0] 正在开放
相机@3be637[id=2] 未知
-------------------------------------------------- ----------------- 打开计数:1(允许的最大数量:1) D/Camera2CameraImpl: {Camera@721ce9d[id=0]} 取消计划重新打开: androidx.camera.camera2.internal.Camera2CameraImpl$StateCallback$ScheduledReopen@a4eea9e D/Camera2CameraImpl: {Camera@721ce9d[id=0]} 打开相机。 D/Camera2CameraImpl: {Camera@721ce9d[id=0]} 正在转换相机内部状态:REOPENING -->开幕 D/CameraStateMachine:新的公共相机状态 CameraState{type=OPENING, error=null} 来自 OPENING 和 null D/UseCaseAttachState:所有用例:[androidx.camera.core.ImageCapture-f0c976a1-2b26-4655-88d6-6790eae98eb6206680605, androidx.camera.core.VideoCapture-c788dcee-3839-4ff7-ad80-908046a7a19e41671826,androidx.camera.core.Preview-45eb99ce-6730-444c-a6dc-e4baabab208b69765876]用于相机:0 D/Camera2CameraImpl: {Camera@721ce9d[id=0]} 由于 validateClientPermissionsLocked:913 无法打开摄像头:调用者“com.example.trucklogger”(PID 10105,UID 5516)无法在没有摄像头的情况下打开摄像头“0”权限 D/Camera2CameraImpl: {Camera@721ce9d[id=0]} 转换相机内部状态:OPENING -->重新开放 D/CameraStateMachine:新的公共相机状态 CameraState{type=OPENING, error=null} 来自 OPENING 和 null D/Camera2CameraImpl:{Camera@721ce9d[id=0]} 尝试在 1000 毫秒内重新打开相机:androidx.camera.camera2.internal.Camera2CameraImpl$StateCallback$ScheduledReopen@4421195 activeResuming = true

知道如何修复它?! 谢谢<3

I'm very newbie in this Android world, and I'm trying to build an app to record video and GPS information.

I'm facing some issues to access the camera. No errors, but it seems permission was not granted.

Manifest: I think there are many redundant lines, but I was trying all possibilities.

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-feature android:name="android.hardware.camera.any" />
    <uses-permission android:name="android.hardware.camera.any" />
    <uses-feature android:name="android.hardware.CAMERA2" />
    <uses-permission android:name="android.permission.CAMERA2" />
    <uses-feature android:name="android.hardware.CAMERAX" />
    <uses-permission android:name="android.permission.CAMERAX" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" android:maxSdkVersion="28"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO"/>

Starting Camera:

  @SuppressLint("RestrictedApi")
    private void cameraXStart(ProcessCameraProvider cameraProvider) {

        cameraProvider.unbindAll();
        CameraSelector cameraSelector = new CameraSelector.Builder()
                .requireLensFacing(CameraSelector.LENS_FACING_BACK)
                .build();
        Preview preview = new Preview.Builder()
                .build();
        preview.setSurfaceProvider(previewView.getSurfaceProvider());

        // Image capture use case
        imageCapture = new ImageCapture.Builder()
                .setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
                .build();

        // Video capture use case
        videoCapture = new VideoCapture.Builder()
                .setVideoFrameRate(30)
                .build();

        //bind to lifecycle:
        cameraProvider.bindToLifecycle((LifecycleOwner) this, cameraSelector, preview, imageCapture, videoCapture);

    }

Then, I have this one to allow camera usage:

ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA}, 100);

When I run, I get thisL

D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Cancelling scheduled re-open: androidx.camera.camera2.internal.Camera2CameraImpl$StateCallback$ScheduledReopen@5296b52
D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Opening camera.
D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Transitioning camera internal state: REOPENING --> OPENING
D/CameraStateMachine: New public camera state CameraState{type=OPENING, error=null} from OPENING and null
D/UseCaseAttachState: All use case: [androidx.camera.core.ImageCapture-f0c976a1-2b26-4655-88d6-6790eae98eb6206680605, androidx.camera.core.VideoCapture-c788dcee-3839-4ff7-ad80-908046a7a19e41671826, androidx.camera.core.Preview-45eb99ce-6730-444c-a6dc-e4baabab208b69765876] for camera: 0
D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Unable to open camera due to validateClientPermissionsLocked:913: Caller "com.example.trucklogger" (PID 10105, UID 5516) cannot open camera "0" without camera permission
D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Transitioning camera internal state: OPENING --> REOPENING
D/CameraStateMachine: New public camera state CameraState{type=OPENING, error=null} from OPENING and null
D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Attempting camera re-open in 1000ms: androidx.camera.camera2.internal.Camera2CameraImpl$StateCallback$ScheduledReopen@a4eea9e activeResuming = true
D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Attempting to force open the camera.
D/CameraStateRegistry: tryOpenCamera(Camera@721ce9d[id=0]) [Available Cameras: 0, Already Open: true (Previous state: OPENING)] --> SUCCESS
D/CameraStateRegistry: Recalculating open cameras:
Camera State
-------------------------------------------------------------------
Camera@dd4530c[id=1] UNKNOWN
Camera@721ce9d[id=0] OPENING
Camera@3be637[id=2] UNKNOWN
-------------------------------------------------------------------
Open count: 1 (Max allowed: 1)
D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Cancelling scheduled re-open: androidx.camera.camera2.internal.Camera2CameraImpl$StateCallback$ScheduledReopen@a4eea9e
D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Opening camera.
D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Transitioning camera internal state: REOPENING --> OPENING
D/CameraStateMachine: New public camera state CameraState{type=OPENING, error=null} from OPENING and null
D/UseCaseAttachState: All use case: [androidx.camera.core.ImageCapture-f0c976a1-2b26-4655-88d6-6790eae98eb6206680605, androidx.camera.core.VideoCapture-c788dcee-3839-4ff7-ad80-908046a7a19e41671826, androidx.camera.core.Preview-45eb99ce-6730-444c-a6dc-e4baabab208b69765876] for camera: 0
D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Unable to open camera due to validateClientPermissionsLocked:913: Caller "com.example.trucklogger" (PID 10105, UID 5516) cannot open camera "0" without camera permission
D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Transitioning camera internal state: OPENING --> REOPENING
D/CameraStateMachine: New public camera state CameraState{type=OPENING, error=null} from OPENING and null
D/Camera2CameraImpl: {Camera@721ce9d[id=0]} Attempting camera re-open in 1000ms: androidx.camera.camera2.internal.Camera2CameraImpl$StateCallback$ScheduledReopen@4421195 activeResuming = true

Any idea how could I fix it?!
Thank you <3

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文