John Doe
John Doe

Reputation: 141

Attempting to open camera causes my app to crash

I have a Button object called takeNewPic that I want to open the camera and then be able to take a picture. However, every time I attempt to do so my app crashes.

 takeNewPic = (Button) findViewById(R.id.ConfigTakeNewPic);
 takeNewPic.setOnClickListener(this);

in my onClick method I called takePicture(), which looks like this:

static final int REQUEST_IMAGE_CAPTURE = 1;

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

In the manifest I have the lines

 <uses-permission-sdk-23 android:name="android.permission.CAMERA" />
 <uses-permission-sdk-23 android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

However, I run into a null pointer exception with an error that reads

java.lang.SecurityException: Permission Denial: starting Intent { 
act=android.media.action.IMAGE_CAPTURE 
cmp=com.motorola.cameraone/com.motorola.camera.Camera } from 
ProcessRecord{dffcd00 28714:com.example.bill.hw2/u0a142} (pid=28714, 
uid=10142) with revoked permission android.permission.CAMERA

Upvotes: 0

Views: 468

Answers (2)

maryS
maryS

Reputation: 31

You have to request permission from the phone itself. Either manually go to app settings and allow app to use camera or use this:

requestPermissions(permissionReq, RECORD_AUDIO_REQUEST_CODE);

Upvotes: 0

user456
user456

Reputation: 262

Have you check this link?

You need to manage the runtime permission as described in the link above.

Upvotes: 1

Related Questions