如何使用onActivityResult检索选定的图像?

问题描述 投票:0回答:1

我有一个要用于进入用户图像库的按钮,他们可以选择图像。然后,我想在页面的imageView中显示它。

 public static final int PICK_IMAGE = 1;
    public void getPhoto(View view){
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);

    startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE) {
        ImageView imageView = findViewById(R.id.imageView);
        imageView.setBackground(requestCode);
    }
}

我已经能够成功选择图像,现在我想使用onActivityResult将我的imageView设置为新图像。

我以为我可以通过使用requestCode,resultCode或data来做到这一点,但是我似乎还不太清楚如何做到这一点。任何指导都是非常好的。

android
1个回答
0
投票
if (requestCode == PICK_IMAGE) {
   if(resultCode==Activity.RESULT_OK){
     Uri file = data.getData(); // you can find path also from here
      ImageView.setImageURI(file)
     }

}

© www.soinside.com 2019 - 2024. All rights reserved.