Photo taken from Android camera error API 21

Asked

Viewed 69 times

0

When making a photo with android camera my app creates a preview of this photo in an Activity where it will be sent by and-mail, however the code below is working perfectly for android below version 4.4.1 and above it at the time of capturing the image path to generate the preview it terminates the app and accuses nullPointerException and the result=-1. Can anyone tell me how to make it work in versions above 4.4.1 android?

Preview where photos will be inserted.

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

Code for the camera:

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);
}
  • Seems to be a permission problem. This code works for me. String path = Environment.getExternalStorageDirectory(). getPath() + "/Android/data/" + getActivity(). getApplicationContext(). getPackageName() + "/files/Pictures/"; File Folder = new File(path); if (!Folder.exists()) { Folder.mkdir(); }

  • getActivity() is not recognized, is asking me to create the method or import android.app.Pendingintent.getActivity(). Which option should I follow?

No answers

Browser other questions tagged

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