如何在Android上使用相机权限解决此错误?

时间:2016-04-09 13:18:36

标签: android

我是一名学生,我被要求制作一个关于图像识别的应用程序,但当我尝试使用相机时,我总是得到:

  

E / CameraService:权限拒绝:无法使用相机

问题是我在清单上添加了权限,并且还使用了运行时权限请求:

public void onScanButtonClicked(View view) {

    String[] permissos = {"android.permission.CAMERA"};

    if (ContextCompat.checkSelfPermission(this,
            Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this,
                permissos,
                MY_PERMISSIONS_REQUEST_CAMERA
        );
    }
      startActivity(new Intent(this, ScanActivity.class));
 }

这是我的清单:

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-feature android:name="android.hardware.camera2" />
    <uses-feature android:name="android.hardware.camera.any" />

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ScanActivity"
        android:label="@string/app_name"

        android:configChanges="orientation|screenSize"
        android:exported="true">
        <uses-permission android:name="android.permission.CAMERA" />
        <uses-feature
            android:name="android.hardware.camera2"
            android:required="false" />
        <uses-feature
            android:name="android.hardware.camera.any"
            android:required="false" />


    </activity>
</application>

2 个答案:

答案 0 :(得分:3)

将uses-permission和uses-feature置于扫描活动的活动和应用程序标记之外

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature android:name="android.hardware.camera2" />
<uses-feature android:name="android.hardware.camera.any" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">


    <activity
        android:name=".MainActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".ScanActivity"
        android:label="@string/app_name"
        android:configChanges="orientation|screenSize"
        android:exported="true">
    </activity>
</application>

同时将if循环更改为

public void onScanButtonClicked(View view) {

String[] permissos = {"android.permission.CAMERA"};

if (ContextCompat.checkSelfPermission(this,
        Manifest.permission.CAMERA)
        != PackageManager.PERMISSION_GRANTED) {

    ActivityCompat.requestPermissions(this,
            permissos,
            MY_PERMISSIONS_REQUEST_CAMERA
    );

}else{
    startActivity(new Intent(this, ScanActivity.class));
}

}

答案 1 :(得分:1)

我有类似的问题。 我的清单文件就像

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

        package="com.example.ildar.textureview">

        <uses-permission android:name="android.permission.CAMERA" />
        <uses-feature android:name="android.hardware.camera" android:required="true"/>
        <uses-feature android:name="android.hardware.camera.autofocus" />

    ....

</manifest>

解决我问题的代码:

if(ContextCompat.checkSelfPermission(this,
                Manifest.permission.CAMERA) == PackageManager.PERMISSION_GRANTED){

            mCamera = Camera.open();
        }else{
            ActivityCompat.requestPermissions(this, new String[]{ Manifest.permission.CAMERA}, 0);
        }

换句话说,如果我没有获得许可,我会以编程方式请求