我设计了一个应用程序,要求允许启动时使用相机。但是,对于Android 6及更高版本,该应用程序崩溃为
allow the access to camera Camera.open() return null
我已将camera.setDisplayOrentaion(90);
设置为返回Null。
答案 0 :(得分:0)
在用户授予权限之前,您不应初始化相机,并且onRequestPermissionsResult
会调用PERMISSION_GRANTED
Activity
。有关如何在应用中正确处理运行时权限的详细信息,请参阅this。
答案 1 :(得分:0)
首先检查是否允许
int permissionCheck = ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.CAMERA);
如果授予了权限,permission check = PackageManager.PERMISSION_GRANTED
或反之亦然PERMISSION_DENIED
。
然后你可以要求它。
if (ContextCompat.checkSelfPermission(thisActivity,
Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Manifest.permission.CAMERA)) {
// Show an explanation 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(thisActivity,
new String[]{Manifest.permission.CAMERA},
MY_PERMISSIONS_REQUEST_CAMERA);
// MY_PERMISSIONS_REQUEST_CAMRA is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
然后你需要处理响应:
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_CAMERA: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Open
// camera, take photo.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}