Pessoal tudo bem?
Estou com problemas no momento que o aplicativo tenta salvar a foto é retornado o seguinte erro
FATAL EXCEPTION: main
Process: br.com.alura.agenda, PID: 9938
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=567, result=-1, data=null} to activity {br.com.alura.agenda/br.com.alura.agenda.FormularioActivity}: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.ImageView
at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassCastException: android.support.v7.widget.AppCompatButton cannot be cast to android.widget.ImageView
at br.com.alura.agenda.FormularioActivity.onActivityResult(FormularioActivity.java:83)
at android.app.Activity.dispatchActivityResult(Activity.java:6428)
at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
at android.app.ActivityThread.-wrap16(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-10 02:47:26.408 9938-9938/br.com.alura.agenda I/Process: Sending signal. PID: 9938 SIG: 9
11-10 02:47:26.593 11109-11109/br.com.alura.agenda W/System: ClassLoader referenced unknown path: /data/app/br.com.alura.agenda-2/lib/x86_64
11-10 02:47:27.943 11109-11109/br.com.alura.agenda W/System: ClassLoader referenced unknown path: /data/app/br.com.alura.agenda-2/lib/x86_64
Segue o codigo para acessar a camera:
/**
* Comando para utilizar a camera
* é passado um setOnClickListener - Classe anonima, e passar a intencao que queremos
* e chamar a tela startActivity
*/
Button botaoFoto = (Button) findViewById(formulario_novaFoto);
botaoFoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
caminhoFoto = getExternalFilesDir(null) + "/" + System.currentTimeMillis() + ".jpg";
File arquivoFoto = new File(caminhoFoto);
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(arquivoFoto));
startActivityForResult(intentCamera, CODIGO_CAMERA);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(resultCode == CODIGO_CAMERA){
//Abre a foto que tiramos
if(requestCode == CODIGO_CAMERA){
ImageView foto = (ImageView) findViewById(formulario_novaFoto);
Bitmap bitmap = BitmapFactory.decodeFile(caminhoFoto);
Bitmap bitmapReduzido = bitmap.createScaledBitmap(bitmap, 300, 300, true);
foto.setImageBitmap(bitmapReduzido);
foto.setScaleType(ImageView.ScaleType.FIT_XY);
}
}
}
Estou usando o emulador do marshmallow, este erro ocorre mesmo usando dispositivo via USB.