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)