How to send a photo taken from the camera to an Imageview?

Asked

Viewed 786 times

0

I need to create an Fragment that can upload images from the camera and also from the gallery to Firebase. However, when I take an image with the camera of the device, the image is not sent to the ImageView from my Ragment. Maybe it’s a very simple mistake, but I can’t 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
        }
    }
}

2 answers

0

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

Try this.

public void ativarCamera(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);
    }
}

0

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

Add permissions from CAMERA, READ_EXTERNAL_STORAGE, MEDIA_CONTENT_CONTROL

and don’t forget it.

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

Browser other questions tagged

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