当前位置: 首页 > 知识库问答 >
问题:

Android M-Camera权限问题

薛鹏飞
2023-03-14

我在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 ProcessRecord{834e30b 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);
        }
    }
}
}

manifest.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个答案

马德宇
2023-03-14

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

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

您可能希望阅读有关运行时权限系统的更多信息。

 类似资料:
  • 我正试图让我的应用程序为新的Android M权限更改做好准备,发现了一些奇怪的行为。我的应用程序使用相机意图机制,允许用户从相机中获得一张照片。但是在另一个活动中,需要使用具有camera权限的camera本身(因为库依赖项card.io需要这样做)。 这是我用来让用户用相机点击一张照片和/或选择一张图像的代码 我在活动中单击按钮时调用。当我没有相机权限在我的应用程序,它的工作很好,但与添加,我

  • 我已经创建了一个webview应用程序,在那里我扫描二维码。在移动和桌面浏览器上一切正常,但在应用程序中,我得到。 我在下面分享一些代码: 我也尝试了这里和这里建议的解决方案,但它对我不起作用。

  • 这是我得到的错误: E180001:无法连接到URL“file://svn/repositories/ncs_svn” \NE180001:无法打开到URL的ra_local会话 \NE180001:无法打开存储库“file://svn/repositories/ncs_svn” \NE000013:无法打开文件“/svn/repositories/ncs_svn/format”:权限被拒绝 我可

  • 我想是因为许可问题。我能修好它吗? 谢谢!

  • 我想使用 null 我想创建自定义组权限,只问我一个请求,只给我一个响应。 谢谢