0
I’m having a problem to save the photo in the Android images folder. Simply it does not save the photo, IE, nor runs the TRY
of ActivityResult
.
He even puts it on ImageView
:
// CAPTURAR FOTO BOTÃO TIRAR FOTO
public void tirarFoto (View view) throws IOException {
SimpleDateFormat nomeFoto = new SimpleDateFormat("dd-MM-yyy_hh-MM-ss");
caminhoFoto = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), nomeFoto + ".jpg");
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
System.out.println(caminhoFoto);
System.out.println(nomeFoto);
startActivityForResult(intent, 0);
}
//JOGA NO ACTIVITY RESULT
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Request Imagem
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
if (data != null) {
Bundle bundle = data.getExtras();
if (bundle != null) {
// Captura o ImageView
ImageView iv = (ImageView)findViewById(R.id.img_camera);
// Redimensiona a Foto
BitmapFactory.Options bmOptions =
new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(
caminhoFoto.getAbsolutePath(), bmOptions);
// Salva a imagem no ImageView
Bitmap img = (Bitmap) bundle.get("data");
iv.setImageBitmap(img);
// SALVA A IMAGEM NO APLICATIVO
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
img.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bytes = stream.toByteArray();
FileOutputStream fos = new FileOutputStream(caminhoFoto);
fos.write(bytes);
fos.close();
Toast.makeText(getApplicationContext(), "Salvou foto", Toast.LENGTH_LONG).show();
} catch (Exception e) {
e.printStackTrace();
}
}
}
} else if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "RESULTADO CANCELADO:\n" + data.getData(), Toast.LENGTH_LONG).show();
}
}