6 votes

Open camera with android application

I'm trying to open the camera from my application, but when I want to open it I get an error, I'm just learning the creation of these applications and I still do not read the errors well

     java.lang.SecurityException: Permission Denial: starting Intent { 
act=android.media.action.IMAGE_CAPTURE 
cmp=com.android.camera2/com.android.camera.CaptureActivity } from 
ProcessRecord{bd3e6b7 4753:com.demonsystem.trackingticket/u0a94} (pid=4753, 
uid=10094) with revoked permission android.permission.CAMERA
 

and the line of my code marks me

  btnCamara.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                startActivityForResult(i,0);
                ocultar();
            }
        });
 

Request for permits

 private void checkCameraPermission() {
        int permissionCheck = ContextCompat.checkSelfPermission(
                this, Manifest.permission.CAMERA);
        if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
            Log.i("Mensaje", "No se tiene permiso para la camara!.");
            ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, 225);
        } else {
            Log.i("Mensaje", "Tienes permiso para usar la camara.");
        }
    }
 

4voto

Joacer Points 4565

You should take a look at the documentation of Android on permissions, it tells you the steps to follow to make a good management of the permissions:

However, the effect of that statement is different according to the version of the system and the level of SDK target of your app:

  • If the device has Android 5.1 or an earlier version, or the level SDK target of your app is the 22 or lower: If you have a dangerous permission in your manifest, the user must grant the permission when installing the app; if you do not grant permission, the system does not will install the app.
  • If the device has Android 6.0 or a later version later, and the level of SDK target of your app is the 23, or one later: The permissions should be listed on the manifest of the app, and this must apply to each dangerous permission that you need while the app is running. The user can grant or deny each permission and the app can continue running with capabilities limited even when the user rejects an application for a permit.

Note: from Android 6.0 (API level 23), users can revoke permissions from any app at any time, although the app is targeted to API level lower. You should test your app to verify that it behaves correctly when you don't have a permission required, regardless of the API level that is oriented your app.

In addition, before Android M the application could launch the camera app without even having camera permissions in your manifest, from Android M if you are declarad permission this must be granted:

Note: if you app targets M and above and declare as using the CAMERA permission which is not granted, then attempting to use this action will result in a SecurityException.

So I have been able to see in your code you make a request for the permissions, but I can't see if you make it when you press the camera button or by entering the activity, regardless of that I see that you need to control the response of the permit application.

Then, I'll leave an example of how to ask permission to press the button of the camera, and how to control the outcome.

Request the permissions that you need

btnCamara.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {

        if (Build.VERSION.SDK_INT>Build.VERSION_CODES.LOLLIPOP_MR1) {// Marshmallow+
            if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)!= PackageManager.PERMISSION_GRANTED) {
                // Should we show an explanation?
                if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.CAMERA)) {
                    // Show an expanation to the user *asynchronously* -- don't block
                    // this thread waiting for the user's response! After the user
                    // sees the explanation, try again to request the permission.
                } else {
                    // No se necesita dar una explicación al usuario, sólo pedimos el permiso.
                    ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.CAMERA}, MY_PERMISSIONS_REQUEST_CAMARA );
                    // MY_PERMISSIONS_REQUEST_CAMARA es una constante definida en la app. El método callback obtiene el resultado de la petición.
                 }
            }else{ //have permissions
                 abrirCamara ();
            }
        }else{ // Pre-Marshmallow
            abrirCamara ();
        }
    }
});

Controls the response to the request of permissions

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_CAMARA : {
            // Si la petición es cancelada, el array resultante estará vacío.
            if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {    
                // El permiso ha sido concedido.
                abrirCamara ();
            } else {    
                // Permiso denegado, deshabilita la funcionalidad que depende de este permiso.
            }
            return;
        }    
        // otros bloques de 'case' para controlar otros permisos de la aplicación
    }
}

Open the camera

public void abrirCamara (){
     Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
     startActivityForResult(i,0);
     ocultar();
}

I hope it helps you.

0voto

M121 Points 113

well in addition to the request for permission in execution time I also put them in the manifest

 <uses-feature android:name="android.hardware.camera" />

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

In the intent of the image I use it like this

 Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
   startActivityForResult(intent, 1);
 

Also if there is an error when asking for the permissions, I add android before manifest "android.manifest.permission.CAMERA"

HolaDevs.com

HolaDevs is an online community of programmers and software lovers.
You can check other people responses or create a new question if you don't find a solution

Powered by:

X