How do I send a photo taken from the camera to an ImageView?

0

I need to create a fragment that can upload images from the camera and also from the gallery to Firebase. However, when I take a picture with the camera of the device, the image is not sent to ImageView of my fragment. Maybe it's a pretty simple mistake, but I can not solve it.

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

    //Se o request for da galeria
    if (requestCode == REQUEST_CODE_GALLERY) {
        if (resultCode == RESULT_OK) {
            if (data != null) {
                //envia o bm da img pro imgview
                try {
                imageUri = data.getData();
                Bitmap bm = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), imageUri);
                imgview.setImageBitmap(bm);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else{
                //fala que deu erro, data = null
            }
        } else {
            //add os alert dialog pra falar que deu erro no result code
        }
    }
    //Se o request for da camera
    if(requestCode == REQUEST_CODE_CAMERA){
        if(requestCode == RESULT_OK){
            if(data != null){
                //comandos para mandar a foto para o imageView
                Bitmap bmc = (Bitmap) data.getExtras().get("data");
                imgview.setImageBitmap(bmc);

            }else{
                //fala que deu erro, data = null, nao ha dados
            }
        }else{
            //add alert dialog falando que deu erro
        }
    }
}
    
asked by anonymous 17.08.2017 / 01:30

2 answers

0

Try to put this code in place. Remembering that the code below is to open gallery and camera and add the photo into an ImageView.

Add permissions from CAMERA, READ_EXTERNAL_STORAGE, MEDIA_CONTENT_CONTROL

And do not forget that.

  static final int REQUEST_IMAGE_CAPTURE = 1;

  static final int PICK_IMAGE_REQUEST = 234;




    public void dispatchTakePictureIntent(View view) {
            Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
                startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
            }
        }


    private void showFileChooser() {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if ((requestCode == PICK_IMAGE_REQUEST || requestCode == REQUEST_IMAGE_CAPTURE) && resultCode == RESULT_OK && data != null && data.getData() != null) {
                filePath = data.getData();
                if (requestCode == PICK_IMAGE_REQUEST) {

                    try {
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
                        imageView.setImageBitmap(bitmap);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                } else if (requestCode == REQUEST_IMAGE_CAPTURE) {
                    filePath = data.getData();
                    Uri selectedImageUri = data.getData();
                    imageView.setImageURI(selectedImageUri);

                }
            }
        }


        public String getFileExtension(Uri uri) {
            ContentResolver cR = getContentResolver();
            MimeTypeMap mime = MimeTypeMap.getSingleton();
            return mime.getExtensionFromMimeType(cR.getType(uri));
        }
    
18.08.2017 / 05:38
0

Use the permission camera uses-permission android: name="android.permission.CAMERA" and external storage uses-permission android: name="android.permission.WRITE_EXTERNAL_STORAGE"

Try this.

public void activateCamera (View view) {

    if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
            == PackageManager.PERMISSION_GRANTED) {
        String nomeFoto = DateFormat.format(
                "yyyy-MM-dd_hhmmss", new Date()).toString();

        imagemSelecionadaUri  = new File(
                Environment.getExternalStoragePublicDirectory(
                        Environment.DIRECTORY_PICTURES),
                nomeFoto);

        Intent it = new Intent(
                MediaStore.ACTION_IMAGE_CAPTURE);
        it.putExtra(MediaStore.EXTRA_OUTPUT,
                Uri.fromFile(imagemSelecionadaUri ));
        startActivityForResult(it, RC_CAMERA);
    }else {
        ActivityCompat.requestPermissions(this, new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE}, 0);
    }
}
    
09.09.2017 / 04:20