简体   繁体   中英

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 Security Exception 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
                setImage();
            } else if (permissionCheck != PackageManager.PERMISSION_GRANTED){
                //Permission not granted, ask for permission
                ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA},
                        REQUEST_CAMERA);
            }
        }
    }

Can anyone see why this keeps crashing? Thanks!

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.

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