event onclik () Error opening camera

2

I'm developing an android application that has an event on Button to open the camera:

In AndroiManifest.xml

    <!-- Permissão para acessar a área de memória externa -->
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

    <!-- Permissão para acessar a câmera diretamente -->
    <uses-permission android:name="android.permission.CAMERA"/>

In MainActivit I have an ImageView and an ImageButton

         <ImageView
            android:id="@+id/imgv_imagem"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageButton
            android:id="@+id/ibtn_tirarfoto"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:onClick="takePicture"
            app:srcCompat="@android:drawable/ic_menu_camera" />

In Activity.java I have the method that fires in the onclick event of the Button

public void takePicture(View v) {
        // Cria uma intent que será usada para abrir a aplicação nativa de câmera
        Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        // Indica na intent o local onde a foto tirada deve ser armazenada
        i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));

        // Abre a aplicação de câmera
        startActivityForResult(i, REQUEST_PICTURE);
    }

The call of the startActivityForResult method is opening an IllegalStateException and gives the following message: "Could not execute method for android: onClick"

The app is closed after this. I apply the same code in another application and it works. Does anyone know what it can be?

Error Log:

11-18 16:41:02.205 30551-30551/br.com.multiwaycursos.multiwayapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.multiwaycursos.multiwayapp, PID: 30551
java.lang.IllegalStateException: Could not execute method for android:onClick
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
   at android.view.View.performClick(View.java:5716)
   at android.widget.TextView.performClick(TextView.java:10926)
   at android.view.View$PerformClick.run(View.java:22596)
   at android.os.Handler.handleCallback(Handler.java:739)
   at android.os.Handler.dispatchMessage(Handler.java:95)
   at android.os.Looper.loop(Looper.java:148)
   at android.app.ActivityThread.main(ActivityThread.java:7325)
   at java.lang.reflect.Method.invoke(Native Method)
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
Caused by: java.lang.reflect.InvocationTargetException
   at java.lang.reflect.Method.invoke(Native Method)
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
   at android.view.View.performClick(View.java:5716) 
   at android.widget.TextView.performClick(TextView.java:10926) 
   at android.view.View$PerformClick.run(View.java:22596) 
   at android.os.Handler.handleCallback(Handler.java:739) 
   at android.os.Handler.dispatchMessage(Handler.java:95) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:7325) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
Caused by: java.lang.SecurityException: Permission Denial: starting Intent { act=android.media.action.IMAGE_CAPTURE cmp=com.sec.android.app.camera/.Camera } from ProcessRecord{c668b6b 30551:br.com.multiwaycursos.multiwayapp/u0a138} (pid=30551, uid=10138) with revoked permission android.permission.CAMERA
   at android.os.Parcel.readException(Parcel.java:1620)
   at android.os.Parcel.readException(Parcel.java:1573)
   at android.app.ActivityManagerProxy.startActivity(ActivityManagerNative.java:3181)
   at android.app.Instrumentation.execStartActivity(Instrumentation.java:1541)
   at android.app.Activity.startActivityForResult(Activity.java:4298)
   at android.support.v4.app.BaseFragmentActivityJB.startActivityForResult(BaseFragmentActivityJB.java:54)
   at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:75)
   at android.app.Activity.startActivityForResult(Activity.java:4245)
   at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:708)
   at br.com.multiwaycursos.multiwayapp.actAutoCadastroCamera.TirarFoto(actAutoCadastroCamera.java:52)
   at java.lang.reflect.Method.invoke(Native Method) 
   at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) 
   at android.view.View.performClick(View.java:5716) 
   at android.widget.TextView.performClick(TextView.java:10926) 
   at android.view.View$PerformClick.run(View.java:22596) 
   at android.os.Handler.handleCallback(Handler.java:739) 
   at android.os.Handler.dispatchMessage(Handler.java:95) 
   at android.os.Looper.loop(Looper.java:148) 
   at android.app.ActivityThread.main(ActivityThread.java:7325) 
   at java.lang.reflect.Method.invoke(Native Method) 
   at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 
   at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) 
    
asked by anonymous 16.11.2017 / 22:06

1 answer

0

Just look at the first Exception (usually has the text Caused by ), in your log see that it contains the error:

  

java.lang.SecurityException: Permission Denial ... with revoked permission android.permission.CAMERA

Complete:

  

Caused by: java.lang.SecurityException: Permission Denial: starting Intent {act = android.media.action.IMAGE_CAPTURE cmp = com.sec.android.app.camera / .Camera} from ProcessRecord {c668b6b 30551: br. com.multiwaycursos.multiwayapp / u0a138} (pid = 30551, uid = 10138) with revoked permission android.permission.CAMERA

It seems that in Android 6.0 the permissions system has changed:

  

link

     

Along with the new functionality of the platform, Android 6.0 Marshmallow has a new permissions model that simplifies the process of installing and updating the application automatically. Permissions are now requested at runtime rather than before the application is installed. In addition, users can choose to deny specific permissions.

An example to request the required permissions:

int permissionCheckStorage = ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA);

if (permissionCheckStorage != PackageManager.PERMISSION_GRANTED)) {
    ActivityCompat.requestPermissions(new String[]{
        Manifest.permission.CAMERA,
        Manifest.permission.WRITE_EXTERNAL_STORAGE,
        Manifest.permission.READ_EXTERNAL_STORAGE
    }, REQUEST_WRITE_STORAGE);
}

Probably missing:

<uses-feature android:name="android.hardware.camera" android:required="true" />
<uses-feature android:name="android.hardware.camera.autofocus" android:required="true" />
Also check if the emulator (if you are using it) is enabled with the camera as I explained in link p>

Extra

One suggestion I make is to check the "package":

public void takePicture(View v) {
    // Cria uma intent que será usada para abrir a aplicação nativa de câmera
    Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    // Certifique-se de que haja uma atividade de câmera para lidar com o Intent
    if (i.resolveActivity(getPackageManager()) != null) {

        // Indica na intent o local onde a foto tirada deve ser armazenada
        i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));

        // Abre a aplicação de câmera
        startActivityForResult(i, REQUEST_PICTURE);
    }
}

    
18.11.2017 / 20:08