简体   繁体   中英

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

Have you check this link ?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM