Android Nougat权限

时间:2017-02-12 11:42:12

标签: permissions camera android-7.1-nougat

伙计我已经从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();
    }
}

这是我的应用程序在启动时的第一个活动,无论我尝试什么它一直保持烘烤权限被拒绝,不会让我授予权限

如果我只是想拍照,它会让应用程序崩溃,所以我知道我需要授权使用相机

任何我想错的想法

任何帮助表示赞赏

标记

1 个答案:

答案 0 :(得分:0)

file://不再允许。 您应该通过content:// scheme发送URI,而不是Content Provider的URI方案。 From Developer Guide

你也可以将目标SDK从24改为23,它会修复它。(不推荐)