Make the Mediastore.INTENT_ACTION_STILL_IMAGE_CAMERA put the photo in an image view

Asked

Viewed 35 times

0

I needed to open the Android Camera App and take the photo. When having the photo taken, put it in an imageview.

My problem is, the client wants to be able to take the photo or else choose from the gallery and I saw that the INTENT_ACTION_STILL_IMAGE_CAMERA puts the gallery options, but it just takes the photo and saves.

It is possible to make the INTENT_ACTION_STILL_IMAGE_CAMERA works in the same way as ACTION_IMAGE_CAPTURE?

1 answer

0


Make it work the same, no, but you can tell where the image is coming from. Remember that permissions should be added:

            PERMISSIONS_STORAGE,
            REQUEST_EXTERNAL_STORAGE

    ...
private final int ALTERAR_IMAGEM_INTENT = 1;
    private Uri fotoCameraUri; //vai gravar o caminho da foto
    ...

This method will add the two cases, file photos or, camera photo.

private void alterarImagem() {

    File fotoCamera = new File(Environment.getExternalStorageDirectory()
            + "/DCIM/", "image" + System.currentTimeMillis() + ".png");

    if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        // Do something for lollipop and above versions
        this.fotoCameraUri = FileProvider.getUriForFile(getApplicationContext(), this.getApplicationContext().getPackageName() + ".provider", fotoCamera);
    } else {
        // do something for phones running an SDK before lollipop
        this.fotoCameraUri = Uri.fromFile(fotoCamera);
    }
    // Camera.
    final List<Intent> cameraIntents = new ArrayList<Intent>();
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    final List<ResolveInfo> listCam = getPackageManager().queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : listCam) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, this.fotoCameraUri);
        cameraIntents.add(intent);
    }

    // Filesystem. Irá verificar
    final Intent galleryIntent = new Intent();
    galleryIntent.setType("image/*");
    galleryIntent.setAction(Intent.ACTION_GET_CONTENT);

    // Chooser of filesystem options.
    final Intent chooserIntent = Intent.createChooser(galleryIntent, getString(R.string.cadastro_concluir_acao_utilizando));

    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

    startActivityForResult(chooserIntent, ALTERAR_IMAGEM_INTENT);
}

In Activity on result you should check where the image came from and set it where it should be added, in your imageview, for example:

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

        if (resultCode == RESULT_OK) {

            final boolean isCamera = ImageUtils.isCamera(data);

            if (!isCamera && (data.getData() == null || this.getContentResolver().getType(data.getData()) == null
                    || !this.getContentResolver().getType(data.getData()).startsWith("image"))) {
                Toast.makeText(this, getString(R.string.cadastro_string_arquivo_selecionando_nao_e_imagem), Toast.LENGTH_LONG).show();
                return;
            }

            Uri selectedImageUri;
            if (isCamera) {
                selectedImageUri = this.fotoCameraUri;
            } else {
                selectedImageUri = data.getData();
            }

            switch (requestCode) {
                case ALTERAR_IMAGEM_INTENT:

                    bitmapImagemUsuario = //faça o decode da sua imagem

                    if (bitmapImagemUsuario != null) {

                        //faça o que tiver de fazer

                        circleImageViewUsuario.setImageBitmap(bitmapImagemUsuario);
                    }

                    break;
            }
        }
    }

It may also be, only the lack of permission, as I do not see your code. I put one here to help more people too.

  • I’ve researched deeper and there’s really no way to make him work the same. What I did then was to call a Alertdialog with the options to grab the image from the gallery or take the photo.

Browser other questions tagged

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