onclik event() Error opening camera

Asked

Viewed 286 times

3

I’m developing an android app I have an event on the Button to open the camera:

On 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"/>

On 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 triggered the Button onclick event

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 startActivityForResult method call is opening an Illegalstateexception and gives the following message "Could not execute method for android:onClick"

The app is powered up 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) 
  • I am using a Navigation Drawer in this actvity, tried in another application that uses the Navigation Drawer and gave the same error. The Navigation Drawer Mainactivity class inherits from the Appcompatactivity class, unlike the others that inherit from the Activity class

  • What is the targetSdkVersion and the Android version of the device you’re testing?

  • Hello ramaral, I use targetSdkVersion 26 on API 21. The project is at https://github.com/faelchristofano/MultiWayApp

  • This could be the problem of runtime permissions or because of changes to permissions that impact file sharing. Put the full bug log.

  • I put the error log

1 answer

0

Just look at the first Exception (usually has the text Caused by), in your log see that 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 on Android 6.0 the permissions system has changed:

https://developers.google.com/android/guides/permissions#prerequisites

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

An example to request the necessary 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 using) is with the camera enabled as I explained in /a/167398/3635

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

  • Thank you Guilherme, I did what you asked, but the error continues. I put the 2 permissions and changed the takePicture method. I’m using my Samsung J5 phone to run. The strange that the code works on other Activity, but this does not want to work.

  • @Rafaelchristófano tries to add <uses-feature android:name="android.hardware.camera.any" android:required="true" />, I’m not sure if it’ll work

  • It didn’t work either...

  • @Rafaelchristófano I think you may have disabled in the settings, or maybe you are using 23+. I will edit the answer

  • @Rafaelchristófano editei, see if you can use/apply.

  • in which class/method I request permissions?

  • @Rafaelchristófano before calling the Intent

Show 3 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.