Android M - 相机权限问题

Posted

技术标签:

【中文标题】Android M - 相机权限问题【英文标题】:Android M - camera permissions issue 【发布时间】:2016-06-30 02:17:56 【问题描述】:

我在 android M 中遇到了已知的权限问题。 尝试打开相机时出现以下异常:

java.lang.SecurityException: Permission Denial: starting Intent 
 act=android.media.action.IMAGE_CAPTURE cmp=com.google.android.GoogleCamera/com.android.camera.CaptureActivity  
from ProcessRecord834e30b 8608:il.ac.shenkar.david.todolistex2/u0a126 (pid=8608, uid=10126) with revoked permission android.permission.CAMERA

这是我的完整代码,其中包含我在这里找到并为其他人工作的解决方法,但由于某种原因对我来说失败了。

public class ReportTaskStatus extends AppCompatActivity

private Task tastToEdit;
private RadioButton acceptrb;
private RadioButton statusrb;
private TextView label;
private List<ParseObject> tsks=null;

public Uri fileUri;
private int CAMERA_REQUEST = 100;
private int Gallary_REQUEST = 101;
public static final int MY_PERMISSIONS_REQUEST_CAMERA = 0;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_report_task_status);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    Intent i = getIntent();


    tastToEdit = (Task)i.getSerializableExtra("task");
    label = (TextView)findViewById(R.id.categorylabel);
    label.append(" "+tastToEdit.getTask_catg().toString());

    label = (TextView)findViewById(R.id.prioritylabel);
    label.append(" "+tastToEdit.getPriority().toString());

    label = (TextView)findViewById(R.id.locationlabel);
    label.append(" " + Location.fromInteger(tastToEdit.getTsk_location()).toString());

    label = (TextView)findViewById(R.id.duetimelabel);

    if(tastToEdit.getDueDate()!=null)
    
        SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
        SimpleDateFormat sdft = new SimpleDateFormat("HH:mm");
        label.append(" " + (sdf.format(tastToEdit.getDueDate())));
        label.append(" " + (sdft.format(tastToEdit.getDueDate())));
    

    Task_Status tsk_stts = tastToEdit.getTask_sts();

    if(tsk_stts==Task_Status.WAITING)
    
        statusrb = (RadioButton) findViewById(R.id.waitingstatusRBtn);
        statusrb.setChecked(true);
    
    if(tsk_stts==Task_Status.INPROGESS)
    
        statusrb = (RadioButton) findViewById(R.id.inprogstatusRBtn);
        statusrb.setChecked(true);
    
    if(tsk_stts==Task_Status.DONE)
    
        statusrb = (RadioButton) findViewById(R.id.donestatusRBtn);
        statusrb.setChecked(true);
    
    //Get a Tracker (should auto-report)
    ((MyApplication) getApplication()).getTracker(MyApplication.TrackerName.APP_TRACKER);

    RadioGroup radioGroup = (RadioGroup) findViewById(R.id.statusgroup);
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() 
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId)
        
            RadioButton rb = (RadioButton) findViewById(R.id.donestatusRBtn);
            if(rb.isChecked())
            
                Log.w("donestatusRBtn","donestatusRBtn");
                openCamera();
            
        
    );


private void Camerapermission() 
    // Here, thisActivity is the current activity
    if (ContextCompat.checkSelfPermission(ReportTaskStatus.this
            ,
            Manifest.permission.CAMERA)
            != PackageManager.PERMISSION_GRANTED) 

        // Should we show an explanation?
        if (ActivityCompat.shouldShowRequestPermissionRationale(ReportTaskStatus.this,
                Manifest.permission.CAMERA)) 

            // Show an expanation to the user *asynchronously* -- don't block
            // this thread waiting for the user's response! After the user
            // sees the explanation, try again to request the permission.

         else 

            // No explanation needed, we can request the permission.

            ActivityCompat.requestPermissions(ReportTaskStatus.this,
                    new String[]Manifest.permission.CAMERA,
                    MY_PERMISSIONS_REQUEST_CAMERA);

            // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
            // app-defined int constant. The callback method gets the
            // result of the request.
        
    


void openCamera() 
    try 
        Intent cameraIntent = new Intent(
        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
     catch (Exception e) 
        e.printStackTrace();
    


@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) 
    switch ( requestCode ) 
        case MY_PERMISSIONS_REQUEST_CAMERA: 
            for( int i = 0; i < permissions.length; i++ ) 
                if( grantResults[i] == PackageManager.PERMISSION_GRANTED ) 
                    Log.d( "Permissions", "Permission Granted: " + permissions[i] );
                 else if( grantResults[i] == PackageManager.PERMISSION_DENIED ) 
                    Log.d( "Permissions", "Permission Denied: " + permissions[i] );
                
            
        
        break;
        default: 
            super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        
    


ma​​nifest.xml 的权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_CALENDAR" />
<uses-permission android:name="android.permission.WRITE_CALENDAR" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.CAMERA" />

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

请帮忙。

【问题讨论】:

【参考方案1】:

首先,您没有调用您的 Camerapermission() 方法,至少就您问题中显示的代码而言。

其次,您允许用户使用触发对openCamera() 的调用的RadioButton 做一些奇怪的事情,而无需检查您是否有权使用相机。例如,您可以使用checkSelfPermission() 来查看您是否有权限,并且只有在您有权限的情况下才启用该RadioButton

您可能希望阅读有关the runtime permission system 的更多信息。

【讨论】:

不敢相信我忘了调用“Camerapermission()”。谢谢。【参考方案2】:

将此添加到您的清单中:

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

文档中的参考:

http://developer.android.com/guide/topics/manifest/uses-feature-element.html#permissions

【讨论】:

【参考方案3】:

也许您使用的是装有 Android M 的设备,因此请为该应用授予适当的权限:设置 -> 应用 -> 您的应用

如果这没有帮助,请查看workaround。

【讨论】:

以上是关于Android M - 相机权限问题的主要内容,如果未能解决你的问题,请参考以下文章

在android M / N中隐藏权限弹出窗口

Android学习之相机权限适配问题详解

Android“权限拒绝:无法使用相机”

Android 10 中的相机意图权限

android M 之前的运行时权限(api 14 及更高版本)

Android\iOS 相机权限 - 移动用户无响应