Tinni C Tinni C - 1 year ago 107
Java Question

Camera Intent keeps crashing Android App

I am writing a simple inventory management app that takes a picture of items to be logged. So the expected behaviour is that users hit a button to launch the camera, take a picture and return to the app to enter text information. But I keep getting the following error

java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=com.android.camera/.Camera } from ProcessRecord{734dbfd 22169:com.virgo19.tinni.teatracker/u0a58} (pid=22169, uid=10058) with revoked permission android.permission.CAMERA

I have looked around the web for days and there doesn't seem to be any solution since I am already following the Android Developer instructions. Including the instruction about asking for permission at runtime. Code fragments below,

//Code calling camera intent
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(cameraIntent, RETURN_FROM_CAMERA);

//On activity return request fragment

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == RETURN_FROM_CAMERA && data != null){
//Check permissions
int permissionCheck = ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA);
if(permissionCheck == PackageManager.PERMISSION_GRANTED){
//Permission is okay, so get on with getting image from Camera
Bundle extras = data.getExtras();
tinImage = (Bitmap) extras.get("data");
//set image view
} else if (permissionCheck != PackageManager.PERMISSION_GRANTED){
//Permission not granted, ask for permission
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},

Can anyone see why this keeps crashing? Thanks!

Answer Source

You need to request the CAMERA permission before taking the picture via startActivityForResult(). Your current code attempts to request this permission after taking the picture.