Photo taken from camera Android API error 21

0

When taking a photo with the android camera my app creates a preview of this photo in an Activity where it will be sent by email, however the code below is working perfectly for the android below version 4.4.1 and above it in the moment to capture the image path to generate the preview it terminates the app and accuses nullPointerException and result = -1. Does anyone know how I can also work on the above versions of android 4.4.1?

Preview where the photos will be inserted.

<LinearLayout
        android:id="@+id/previewFoto"
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
</LinearLayout>

Camera Code:

private String caminhoFoto = "";

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_send__email);
  camera.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Intent intentCamera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            caminhoFoto = getExternalFilesDir(null) + "/" + String.valueOf(System.currentTimeMillis()) + ".jpg";
            Log.i("CAMINHO",caminhoFoto);
            File arquivoFoto = new File(caminhoFoto);
            intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(arquivoFoto));
            startActivityForResult(intentCamera, CODIGO_CAMERA);
        }
    });

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    if(resultCode == Activity.RESULT_OK){
        if(requestCode == CODIGO_CAMERA){
            carregaImagem(caminhoFoto);
        } else if(requestCode == CODIGO_GALERIA){
            try{
                listaImagem(intent);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
    }
}
}
 private void carregaImagem(String caminhoFoto) {

    Bitmap bitmap = BitmapFactory.decodeFile(caminhoFoto);
    Bitmap bitmapReduzido = Bitmap.createScaledBitmap(bitmap, 300,300, true);

    LinearLayout lista = (LinearLayout) findViewById(R.id.previewFoto);

    ImageView foto = new ImageView(this);
    foto.setImageBitmap(bitmapReduzido);
    foto.setScaleType(ImageView.ScaleType.FIT_XY);

    lista.addView(foto);
}
    
asked by anonymous 13.10.2016 / 16:59

0 answers