Why, sometimes, the URI passed in EXTRA_OUTPUT is null in onActivityResult?

0

A variable is declared at the beginning of the class:

private Uri UriPhoto;

When the user selects the button to take the photo the function below is called:

public void takePicture() {
    try {
        File localFile = new File(this.dirFotos);
        if (localFile.mkdirs() || localFile.exists()) {
            this.UriPhoto = Uri.fromFile(new File(localFile, ".h" + this.selecao + this.arq));
            Intent localIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            localIntent.putExtra(EXTRA_OUTPUT, this.UriPhoto);
            startActivityForResult(localIntent, 1);
        } else {
            Log.e(tag, "localFile não existe e não pode ser criado");
        }

    } catch (Exception localException) {
        Log.e(tag, "catch takepicture");
        Log.v(getClass().getSimpleName(), localException.getMessage());
    }
}

In onActivityResult the application works with the saved image:

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
            if ((this.UriPhoto != null) && (this.UriPhoto.getPath() != null) && (!"".equals(this.UriPhoto.getPath()))) {
                Log.e(tag, "URI SIM");

                if (Image.resizeFoto(this.UriPhoto.getPath(), 400, 400) != null) {
                    Log.e(tag, "Redimensionu true");
                }

                ClassFoto foto = ClassFoto.getInstancia();

                int indice = foto.getProximoIndice(context);

                ContentValues valores = new ContentValues();
                valores.put("cod_visita", this.cod_visita);
                valores.put("cod_cliente", this.cod_cliente);
                valores.put("indice", indice);
                valores.put("nome_arquivo", this.UriPhoto.getLastPathSegment());
                valores.put("foto_tipo", this.selecao);
                valores.put("offline", this.offline);

                foto.insertFoto(this.context, valores);
                //refreshResult();
                //onResume();

                Intent intent;

                intent = new Intent(this.context, ActivityFotoGrid.class);
                intent.putExtra("position", position);

                overridePendingTransition(R.anim.left_in, R.anim.right_out);
                startActivity(intent);

                finish();
            } else {
                Log.e(tag, "URI NÃO");
            }
        }
    }
}

This code works fine most of the time. Only in some very random situations, the line below is false:

if ((this.UriPhoto != null) && (this.UriPhoto.getPath() != null) && (!"".equals(this.UriPhoto.getPath())))

When UriPhoto was set to EXTRA_OUTPUT there was the path of the image correctly because if I go in the folder the image is there, but the above line being false shows that at some point the UriPhoto variable has lost its contents and so the application can not do it What do you need with the photo?

This variable is not used in any other part of the code, only in these two methods. Could anyone tell me what happens in the transition between the camera and the Activity can cause this variable to lose its content and be null or void?

Note: This happens in very few cases, most of the time it works normally and I could never see anything different in the use that justifies cleaning the variable.

Thanks in advance for your attention.

    
asked by anonymous 19.04.2018 / 14:48

0 answers