簡體   English   中英

從圖庫上傳圖像並在 AlertDialog 中顯示

[英]Upload image from gallery and display in 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:///”添加到畢加索,但這不起作用。

謝謝。

為什么不直接使用Uri來顯示圖片呢? 就像:

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

同時,值得注意的是

解析器.query()

是一個 I/O 操作。 不建議直接在主線程上操作

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM