public void setImagemComunicado(){
ImageButton botaoFoto = (ImageButton) findViewById(R.id.Comunicado_botao_foto);
botaoFoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(intentCamera.resolveActivity(getPackageManager()) != null) {
File pasta = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
File imagem = new File((pasta.getPath() + File.separator + "JPG_" + System.currentTimeMillis() + ".jpg"));
if (imagem != null) {
caminhoFoto = imagem;
Uri imgUri = FileProvider.getUriForFile(getBaseContext(), getBaseContext().getApplicationContext().getPackageName() + ".provider", imagem);
intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
startActivityForResult(intentCamera, CODIGO_CAMERA);
}
}
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CODIGO_CAMERA && resultCode == RESULT_OK && caminhoFoto != null) {
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(caminhoFoto)));
exibirImagem();
}
}
private void exibirImagem() {
btnImagem = (ImageButton) findViewById(R.id.Comunicado_botao_foto);
int targetW = btnImagem.getWidth();
int targetH = btnImagem.getHeight();
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(caminhoFoto.getAbsolutePath(), bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
int scaleFactor = Math.min(photoW/targetW,photoH/targetH);
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
Bitmap bitmap = BitmapFactory.decodeFile(caminhoFoto.getAbsolutePath(), bmOptions);
btnImagem.setImageBitmap(bitmap);
}