简体   繁体   English

我无法从片段中的图库中选择图像

[英]I can't pick image from gallery in fragment

I have a dialog in my fragment there is a ImageView in the dialog, I want to click on image and pick a image from gallery but this code doesn't work in fragment but it work in activity.我的片段中有一个对话框,对话框中有一个 ImageView,我想单击图像并从图库中选择一个图像,但是此代码在片段中不起作用,但在活动中起作用。

when I click On ImageView this code must execute but nothing happining.当我单击 On ImageView 时,此代码必须执行,但没有发生任何事情。

private void pickContactImage(){
    Intent intent = new Intent(Intent.ACTION_PICK);
    intent.setType("image/*");
    startActivityForResult(intent,PICK_IMAGE_CODE);
}
@Override 
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_CODE && resultCode == RESULT_OK){
        imageUri = data.getData();
        contactImageView.setImageURI(data.getData());
    }
}

try this尝试这个

call the function pickContactImage() in button click在按钮单击中调用函数pickContactImage()

private void pickContactImage(){
   Intent intent = new Intent(Intent.ACTION_PICK);
   intent.setType("image/*");
   startActivityForResult(intent,100);
  }

@Override 
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) 
{
  super.onActivityResult(requestCode, resultCode, data);
  if (requestCode == 100){
   if (data.getData() != null){
        contactImageView.setImageURI(data.getData());
      }
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM