从画廊上传图像,并在Alertdialog中显示

发布于 2025-02-13 22:51:31 字数 1469 浏览 0 评论 0原文

我试图在AlertDialog中显示从画廊上传的图像,但现在对我不起作用。

我的代码:

private void dispatchTakePictureIntent() {
        Intent imageGetter = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        activityLauncher.launch(imageGetter);
    }

ActivityResultLauncher<Intent> activityLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> {
        Uri selectedImage = result.getData().getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};//Array size of 1, and we put in a string
        Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String user_image_path = cursor.getString(columnIndex);
        myImageView = new ImageView(getContext());
        Picasso.get().load(user_image_path).fit().centerCrop().into(myImageView);
        AlertDialog.Builder builder =
                new AlertDialog.Builder(getContext()).
                        setMessage("Message above the image").
                        setPositiveButton("OK", (dialog, which) -> dialog.dismiss()).
                        setView(myImageView);
        builder.create().show();
        cursor.close();
    });

画廊打开,我可以选择一张图片,但是在选择图片后,它会返回,而没有图像的空白对话框(邮件就在那里)。

我尝试在毕加索添加“ file:///”,但这不起作用。

谢谢。

I'm trying to display an image uploaded from the gallery in an AlertDialog but it's not working for me right now.

My code:

private void dispatchTakePictureIntent() {
        Intent imageGetter = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        activityLauncher.launch(imageGetter);
    }

ActivityResultLauncher<Intent> activityLauncher = registerForActivityResult( new ActivityResultContracts.StartActivityForResult(), result -> {
        Uri selectedImage = result.getData().getData();
        String[] filePathColumn = {MediaStore.Images.Media.DATA};//Array size of 1, and we put in a string
        Cursor cursor = getContext().getContentResolver().query(selectedImage, filePathColumn, null, null, null);
        cursor.moveToFirst();
        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String user_image_path = cursor.getString(columnIndex);
        myImageView = new ImageView(getContext());
        Picasso.get().load(user_image_path).fit().centerCrop().into(myImageView);
        AlertDialog.Builder builder =
                new AlertDialog.Builder(getContext()).
                        setMessage("Message above the image").
                        setPositiveButton("OK", (dialog, which) -> dialog.dismiss()).
                        setView(myImageView);
        builder.create().show();
        cursor.close();
    });

The gallery opens up and I can choose a picture but after I choose a picture it goes back and a blank dialog with no image (the message is there) shows up.

I tried adding "file:/// " to the Picasso but that doesn't work.

Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

回眸一遍 2025-02-20 22:51:31

为什么不直接使用URI显示图片?
就像:

Uri selectedImage = result.getData().getData();
Picasso.get().load(selectedImage).fit().centerCrop().into(myImageView);

同时,值得注意的是

resolver.query()

是I/O操作。不建议直接在主线程上操作

Why not use the Uri directly to display the picture?
Just like:

Uri selectedImage = result.getData().getData();
Picasso.get().load(selectedImage).fit().centerCrop().into(myImageView);

At the same time, it is worth noting that

resolver.query()

is a I/O operation. It is not recommended to operate directly on the main thread

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文