Android 牛轧糖许可
Android Nougat Permission
伙计们,我已经从 Lollypop 转到了 Nougat,我正在尝试让我的相机在我的应用程序中拍照
我了解到您现在必须在 运行 时授予权限并已尝试以下操作
static final Integer CAMERA = 0x5;
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
askForPermission(Manifest.permission.CAMERA,CAMERA);
}
private void askForPermission(String permission, Integer requestCode) {
Toast.makeText(this, permission, Toast.LENGTH_SHORT).show();
if (ContextCompat.checkSelfPermission(newstart.this, permission) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(newstart.this, permission)) {
//This is called if user has denied the permission before
//In this case I am just asking the permission again
ActivityCompat.requestPermissions(newstart.this, new String[]{permission}, requestCode);
} else {
ActivityCompat.requestPermissions(newstart.this, new String[]{permission}, requestCode);
}
} else {
Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Permission check", Toast.LENGTH_SHORT).show();
switch (requestCode) {
//Location
case 1:
break;
//Write external Storage
case 3:
break;
//Read External Storage
case 4:
Intent imageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(imageIntent, 11);
break;
//Camera
case 5:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, 12);
}
break;
}
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}
这是我的应用程序启动时的第一个 activity,无论我尝试什么它都会拒绝烘烤权限并且不会让我授予权限
如果我只是尝试拍照,应用程序就会崩溃,所以我知道我需要授予使用相机的权限
任何错误的想法
感谢任何帮助
马克
file:// 不再被允许。
您应该通过 content:// scheme 发送 URI,而不是 Content Provider 的 URI scheme。
From Developer Guide
您也可以将目标 SDK 从 24 更改为 23 它会修复它。(不推荐)
伙计们,我已经从 Lollypop 转到了 Nougat,我正在尝试让我的相机在我的应用程序中拍照
我了解到您现在必须在 运行 时授予权限并已尝试以下操作
static final Integer CAMERA = 0x5;
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
askForPermission(Manifest.permission.CAMERA,CAMERA);
}
private void askForPermission(String permission, Integer requestCode) {
Toast.makeText(this, permission, Toast.LENGTH_SHORT).show();
if (ContextCompat.checkSelfPermission(newstart.this, permission) != PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(newstart.this, permission)) {
//This is called if user has denied the permission before
//In this case I am just asking the permission again
ActivityCompat.requestPermissions(newstart.this, new String[]{permission}, requestCode);
} else {
ActivityCompat.requestPermissions(newstart.this, new String[]{permission}, requestCode);
}
} else {
Toast.makeText(this, "" + permission + " is already granted.", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(ActivityCompat.checkSelfPermission(this, permissions[0]) == PackageManager.PERMISSION_GRANTED){
Toast.makeText(this, "Permission check", Toast.LENGTH_SHORT).show();
switch (requestCode) {
//Location
case 1:
break;
//Write external Storage
case 3:
break;
//Read External Storage
case 4:
Intent imageIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(imageIntent, 11);
break;
//Camera
case 5:
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, 12);
}
break;
}
Toast.makeText(this, "Permission granted", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "Permission denied", Toast.LENGTH_SHORT).show();
}
}
这是我的应用程序启动时的第一个 activity,无论我尝试什么它都会拒绝烘烤权限并且不会让我授予权限
如果我只是尝试拍照,应用程序就会崩溃,所以我知道我需要授予使用相机的权限
任何错误的想法
感谢任何帮助
马克
file:// 不再被允许。 您应该通过 content:// scheme 发送 URI,而不是 Content Provider 的 URI scheme。 From Developer Guide
您也可以将目标 SDK 从 24 更改为 23 它会修复它。(不推荐)