作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
这个问题相信很多人早就问过了。现在,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/
我正在尝试调试一个不是我自己编写的应用程序。最后一个问题是 moment.js 折旧错误:折旧警告:moment 构建回退到 js Date。这是不鼓励的,并将在即将发布的主要版本中删除。请引用htt
关闭。这个问题需要更多focused .它目前不接受答案。 想改进这个问题吗? 更新问题,使其只关注一个问题 editing this post . 关闭 8 年前。 Improve this q
我是一名优秀的程序员,十分优秀!