伙计们,我从 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();
}
}
这是我的应用程序启动时的第一个活动,无论我尝试什么,它都会拒绝烤面包权限,不会让我授予权限
如果我只是尝试拍照,它会使应用程序崩溃,所以我知道我需要授予使用相机的权限
任何想法我要去错了
任何帮助表示赞赏
标记