首页 > 解决方案 > 如何获得相机权限android

问题描述

我正在尝试获得在 android 设备上使用 ffmpeg 录制视频的权限。

这是我的代码AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.camera">
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/Theme.Camera">
        <activity android:name=".DisplayMessageActivity"
            android:parentActivityName=".MainActivity">
            <!-- The meta-data tag is required if you support API level 15 and lower -->
            <meta-data
                android:name="android.support.PARENT_ACTIVITY"
                android:value=".MainActivity" />
        </activity>        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

Here is the specific camera command in 'MainActivity.kt`:
   FFmpeg.execute("-f android_camera -i 0:0 -r 30 -pixel_format bgr0 -t 00:00:05")

这是我的错误:

021-03-22 12:40:41.679 25156-25156/com.example.camera E/ACameraMetadata: filterDurations: malformed duration key 1638402! count 0, type 3
2021-03-22 12:40:41.679 25156-25156/com.example.camera E/ACameraMetadata: filterDurations: malformed duration key 1638403! count 0, type 3
2021-03-22 12:40:41.679 25156-25156/com.example.camera E/ACameraMetadata: filterDurations: malformed duration key 1835009! count 0, type 3
2021-03-22 12:40:41.679 25156-25156/com.example.camera E/ACameraMetadata: filterDurations: malformed duration key 1835010! count 0, type 3
2021-03-22 12:40:41.679 25156-25156/com.example.camera E/ACameraMetadata: filterDurations: malformed duration key 1638407! count 0, type 3
2021-03-22 12:40:41.679 25156-25156/com.example.camera E/ACameraMetadata: filterDurations: malformed duration key 1638408! count 0, type 0
2021-03-22 12:40:41.679 25156-25156/com.example.camera E/ACameraMetadata: filterDurations: malformed duration key 1638402! count 0, type 3
2021-03-22 12:40:41.679 25156-25156/com.example.camera E/ACameraMetadata: filterDurations: malformed duration key 1638403! count 0, type 3
2021-03-22 12:40:41.679 25156-25156/com.example.camera E/ACameraMetadata: filterDurations: malformed duration key 1835009! count 0, type 3
2021-03-22 12:40:41.679 25156-25156/com.example.camera E/ACameraMetadata: filterDurations: malformed duration key 1835010! count 0, type 3
2021-03-22 12:40:41.679 25156-25156/com.example.camera E/ACameraMetadata: filterDurations: malformed duration key 1638407! count 0, type 3
2021-03-22 12:40:41.679 25156-25156/com.example.camera E/ACameraMetadata: filterDurations: malformed duration key 1638408! count 0, type 0
2021-03-22 12:40:41.681 25156-25156/com.example.camera E/ACameraManager: openCamera: connect camera device failed: Status(-8, EX_SERVICE_SPECIFIC): '1: validateClientPermissionsLocked:1041: Caller "" (PID 10222, UID 25156) cannot open camera "0" without camera permission'
2021-03-22 12:40:41.681 25156-25323/com.example.camera E/mobile-ffmpeg: [android_camera @ 0x7267a03e00] Failed to open camera with id 0, error: ACAMERA_ERROR_PERMISSION_DENIED.
2021-03-22 12:40:41.681 25156-25323/com.example.camera E/mobile-ffmpeg: [android_camera @ 0x7267a03e00] Failed to open camera.
2021-03-22 12:40:41.682 25156-25323/com.example.camera E/mobile-ffmpeg: [android_camera @ 0x7267a03e00] Failed to open android_camera.
2021-03-22 12:40:41.682 25156-25323/com.example.camera E/mobile-ffmpeg: 0:0: Generic error in an external library
2021-03-22 12:40:41.707 25156-25156/com.example.camera W/ActivityThread: handleWindowVisibility: no activity for token android.os.BinderProxy@4ac3524

我究竟做错了什么?我查看的每个来源都建议我在上面添加的权限,但它不起作用。

标签: androidandroid-studioffmpegandroid-ffmpegandroid-video-record

解决方案


在使用相机之前,您需要添加这些代码行:

    if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 0);

它将要求用户允许应用程序使用相机。


推荐阅读