android bottomNavigationView项的startActivityForResult已弃用的解决方案是什么?

sr4lhrrt  于 2023-04-04  发布在  Android
关注(0)|答案(1)|浏览(144)

1我想从底部导航视图中选择一个选定的项目(在这种情况下,它是R.id.status),并在名为statusList的recyclerview中显示图像。如果startActivityForResult已被弃用,我该怎么做?

rqmkfv5c

rqmkfv5c1#

首先需要声明private ActivityResultLauncher<String> imagePickerLauncher;
之后,这里是如何启动图像选择器

// Launch the image picker
        imagePickerLauncher.launch("image/*");

为了检索结果,您复制以下代码

// Initialize the imagePickerLauncher
        imagePickerLauncher = registerForActivityResult(new 
        ActivityResultContracts.GetContent(), uri -> {
            if (uri != null) {
                 // Do something with the selected image URI
                 // For example, add it to the RecyclerView adapter
                
             }
         });

相关问题