gpt4 book ai didi

android 从图库中选择图像(现在 startActivityForResult 已贬值)

转载 作者:行者123 更新时间:2023-12-05 06:02:30 26 4
gpt4 key购买 nike

这个问题相信很多人早就问过了。现在,startActivityForResult 已贬值,我正在寻找它的替代品。

以前的代码是

        public void onClick(View v) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(
Intent.createChooser(
intent,
"Select Image from here..."),
PICK_CODE);

最佳答案

使用这个,

ImageView image;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

image = (ImageView) findViewById(R.id.display_image);
}

public void pickAImage(View view)
{
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
someActivityResultLauncher.launch(photoPickerIntent);
}

然后进行操作,

ActivityResultLauncher<Intent> someActivityResultLauncher = registerForActivityResult(
new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
// There are no request codes
// doSomeOperations();
Intent data = result.getData();
Uri selectedImage = Objects.requireNonNull(data).getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
BitmapFactory.decodeStream(imageStream);
image.setImageURI(selectedImage);// To display selected image in image view
}
});

关于android 从图库中选择图像(现在 startActivityForResult 已贬值),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66908673/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com