如何从外部 Intent 裁剪图像

问题描述

TLDR 因此,当我尝试在我的应用程序中共享来自另一个应用程序的图像时,我想使用所选图像打开裁剪意图

在我的应用中,可以通过共享意图共享来自另一个应用的图像。 所以例如您可以直接在我的应用中分享来自 Reddit 的图片。 它工作得很好,但我想添加一个功能,您可以裁剪图像,因为有些图像的上方和下方都有巨大的黑屏(可能因为它们只是屏幕截图)。

我可以裁剪从图库拍摄的图像。 但是,当我从外部意图使用共享时,图像不会保存到我的手机中,因此我无法打开它并对其进行裁剪。

我的代码如何获取图像

    private void getData() {
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if (type.startsWith("image/")) {
            handleSendImage(intent);
        }
        if (type.startsWith("text/")) {
            handleSendText(intent);
        }
    }
}


void handleSendImage(Intent intent) {
    imageUrl = intent.getParcelableExtra(Intent.EXTRA_STREAM);

    if (imageUrl != null) {

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        try {
            BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUrl),null,options);
        } catch (FileNotFoundException e) {
            e.printstacktrace();
        }
        choose_image_iv.setColorFilter(0);
        Picasso.get().load(imageUrl).fit().into(choose_image_iv);

        try {
            Bitmap img = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUrl);
            if (img == null) return;
            int w = img.getWidth();
            int h = img.getHeight();

            rat = (double) w / (double) (h);

        } catch (IOException e) {
            e.printstacktrace();
        }

        if (rat == 0.0) {
            rat = 1.0;
        }

        rightNavButton.setEnabled(true);
        rightNavButton.setimageResource(R.drawable.baseline_navigate_next_black_24dp);
        rightNavButton.setColorFilter(ContextCompat.getColor(ShareFromOutsideActivity.this,R.color.AppColor));
        titleView.setText(R.string.choose_category);
        post_text_text_tv.requestFocus();
    }else {
        new SweetAlertDialog(this,SweetAlertDialog.ERROR_TYPE)
                .setTitleText("Mit diesem Bild scheint etwas nicht zu stimmen. Probier ein anderes").show();

    }
}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)