从画廊上传图像,并在Alertdialog中显示
我试图在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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为什么不直接使用URI显示图片?
就像:
同时,值得注意的是
是I/O操作。不建议直接在主线程上操作
Why not use the Uri directly to display the picture?
Just like:
At the same time, it is worth noting that
is a I/O operation. It is not recommended to operate directly on the main thread