Search code examples
permissionscameraandroid-7.1-nougat

Android Nougat Permission


Guys I have gone from Lollypop to Nougat and am trying to get my camera to take a picture in my app

I understand you now have to grant permissions at run time and have tried the following

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();
    }
}

This is the first activity in my app on startup and no matter what I try It keeps toasting permission denied and will not let me grant permissions

If I try just to take a photo it crashes the app so I know I need to grant permission to use camera

Any Ideas where I'm going Wrong

Any help appreciated

Mark


Solution

  • file:// is not allowed anymore. You should send the URI through content:// scheme instead which is the URI scheme for Content Provider. From Developer Guide

    you can also change the target SDK from 24 to 23 it will fix it.(not recommended)