In order to be able to capture an image by camera, Voce must first create an input by the code similar to yours :
private static final CAMERA=1; //o actionCode que voce usara no onActivityResult
private void intentParaTirarFoto(int codigoDePedido) {
Intent intentParaTirarFoto= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intentParaTirarFoto, codigoDePedido);
}
After that in the method on Activityresult Voce makes:
@Override
public void onActivityResult(int codigoDePedido, int resultado, Intent dados) {
super.onActivityResult(codigoDePedido, resultado, dados);
if(codigoDePedido==CAMERA && resultado==getActivity().RESULT_OK)
{
Bitmap bitmapImage=(Bitmap)data.getExtras().get("data");
//apartir daqui voce pode gravar a imagem ou fazer alguma outra coisa com o bitmap
gravarImagem(bitmapImage);
}
}
below the implementation of the code to record the image on mobile:
private void gravarImagem(Bitmap bitmap) {
String root = Environment.getExternalStorageDirectory().toString();
File file;
File minhaPasta= new File(root + "/Pictures/Testando");
Date date=new Date();
long timeStamp=date.getTime();
if(!myDir.exists())
{
myDir.mkdir(); //cria uma nova pasta caso a espicificada acima nao exista
}
String nomeFicheiro= "TestandoCamera"+timeStamp+".jpg";
file= new File (myDir, fname);
if (file.exists ()) file.delete ();
try {
FileOutputStream out = new FileOutputStream(file);
finalBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
out.flush();
out.close();
} catch (Exception e) {
e.printStackTrace();
}
Log.e("GRAVOU", "IMAGEM GRAVADA NO CELULAR");
}
Whenever possible post the logcat text. This image is practically unreadable.
– Androiderson
I tested your code snippet and it worked normally. Make sure the snippet
Environment.getExternalStorageDirectory()
does not return null. Only one detail I found is that your file has no extension, but this should not influence.– Alexandre Atoji
Thanks, I’ll check in.
– Elivyhe
@Exceptional even agree with you, logcat text is better, but the image is not unscathed, it is the OS that reduces the image to fit in the layout. Just click your mouse and save it.
– Guilherme Nascimento
Have you tested on more than one device or just on the emulator? Some phones (I can’t remember exactly which ones now) return null to that call.
– Neto Marin
The call
Environment.getExternalStorageDirectory()
can actually return null. But, this can happen when the device does not have a Externalstorage configured or enabled. To avoid null output, callEnvironment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)
to know if it is possible to access the Externalstorage. More information can be found on android developers– Igor Castañeda Ferreira