android - 如何获得相机权限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
我究竟做错了什么?我查看的每个来源都建议我在上面添加的权限,但它不起作用。
解决方案
在使用相机之前,您需要添加这些代码行:
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 0);
它将要求用户允许应用程序使用相机。
推荐阅读
- c - 我需要释放 C 中本地变量的内存吗?
- ios - 如何在 AppAuth iOS 中将“id_token”作为 responseType 而不是“code”传递来获取令牌
- java - if 语句抛出不兼容的类型:意外的返回值
- google-app-engine - TypeError:无法过滤非节点参数;收到错误
- amazon-web-services - ec2.SubnetType 部署到 2AZs 而不是 3
- mysql - 无法理解 SQL 查询
- python - 带有额外输入数据的 tensorflow 自定义损失函数
- c++ - 为什么指针不写类的地址?
- javascript - React Native 等待承诺永远不会执行
- python - 动态加载文件,并在其中运行函数