-1

I am trying to get access to the camera from within the app. I have the following code and all is working except it's not giving the user the option to grant permissions. Not quite sure what has gone wrong with it.

public void takePicture (View view) {

    if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)
    {
       //requestPermissions(new String[]{Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);
        ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.CAMERA}, MY_CAMERA_PERMISSION_CODE);

    }
    else
    {
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);
    }


}

public void saveButton (View view) {
    Log.i("info", "stuff");
}

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)
{
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
    if (requestCode == MY_CAMERA_PERMISSION_CODE)
    {
        if (grantResults[0] == PackageManager.PERMISSION_GRANTED)
        {
            Toast.makeText(this, "camera permission granted", Toast.LENGTH_LONG).show();
            Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(cameraIntent, CAMERA_REQUEST);
        }
        else
        {
            Toast.makeText(this, "camera permission denied", Toast.LENGTH_LONG).show();
        }
    }
}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
        Bitmap photo = (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(photo);
    }
}

Once the button is clicked the toast comes up to show that permissions have not been granted but no option to grant them in the first place?

MurugananthamS
  • 2,395
  • 4
  • 20
  • 49
greenpony
  • 17
  • 7
  • Does this answer your question? [How get permission for camera in android.(Specifically Marshmallow)](https://stackoverflow.com/questions/38552144/how-get-permission-for-camera-in-android-specifically-marshmallow) – a_local_nobody Nov 08 '19 at 12:08
  • I hope you're aware of the fact that the user won't be asked to allow the camera permission if he/she has already allowed – OMi Shah Nov 08 '19 at 12:09
  • It's not giving the option to allow permission in the first place – greenpony Nov 08 '19 at 20:31

1 Answers1

0

Please make sure you have added camera permission in Androidmanifeast.xml and minor change in your code replace below line

if (checkSelfPermission(Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) 

with

if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED)