迁移StartActivityForResult后,onActivityResult中出现错误



我修改了代码,使其与On活动结果一起工作,但我在profileImageView.setImageURI(ImageUri(行中遇到了一个无法更正的错误;

他告诉我,他需要乌里,它来自意向

ActivityResultLauncher<Intent> mGetContent=registerForActivityResult(new 
ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() 
{
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode()==RESULT_OK && result.getData()!=null){
Intent ImageUri=result.getData();
profileImageView.setImageURI(ImageUri);
}
}
});

public void onClick(View v) {
Intent galleryIntent = new Intent();
galleryIntent.setAction(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
//    startActivityForResult(galleryIntent,GalleryPick);
mGetContent.launch(galleryIntent);
}

感谢您的帮助

请在onActivityResult((方法内按如下方式更改代码。

if(result.getResultCode() == Activity.RESULT_OK){
Intent data = result.getData();
if(data != null){
Uri imageUri = data.getData();
if(imageUri != null){
try{
InputStream inputStream = YourActivityName.this.getContentResolver().openInputStream(imageUri);
BitMap bitMap = BitmapFactory.decodeStream(inputStream);
profileImageView.setImageBitmap(bitMap);
}catch(Exception e){
e.printStackTrace();
}
} 
}
}

而且,您的OnClick((方法应该如下所示:

public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
mGetContent.launch(intent);
}

相关内容

  • 没有找到相关文章

最新更新