How to get camera permission android

4.4k Views Asked by At

I am trying to get permission to record video with ffmpeg on an android device.

Here is my code from 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")

Here is my error:

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

What am I doing wrong? Every source I look at suggest the permissions I added above but it doesn't work.

1

There are 1 best solutions below

0
On

You'll need to add those lines of code before your using the camera:

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

it will ask the user to allow the application to use the camera.