App crashing when an image is selected

Asked

Viewed 258 times

5

I am trying to get an image using an Intent, but when I select the image, my application closes immediately. This is my current code:

private void capturarFoto() {

    String nameFoto = DateFormat.format("yyyy-MM-dd_hhmmss", new Date()).toString();

    caminhoFoto = new File(Environment.getExternalStorageDirectory(),nameFoto);

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(caminhoFoto));
    getActivity().startActivityForResult(intent, 1);
}

He must call the method onActivityResult after image selection, but unfortunately closes without any error in Logcat.

Is there something wrong?

LOGCAT LOGCAT

  • 1

    Whenever possible post the logcat text. This image is practically unreadable.

  • 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.

  • Thanks, I’ll check in.

  • @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.

  • 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.

  • 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, call Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED) to know if it is possible to access the Externalstorage. More information can be found on android developers

Show 1 more comment

2 answers

1

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");
}

0

How are you accessing the getActivity(). startActivityForResult(Intent, 1); I believe you’re accessing this code through a fragment. Note that as you are calling this method via Activity, you should expect the onActivityResult of your Activity be called and not the onActivityResult of your Fragment. Can you verify this? From the informed data, is what can be deduced.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.