从Gallery获取图片

时间:2017-08-27 05:00:30

标签: android

当我从图库中提取图像时,我在SELECT_PICTURE上收到错误。 有人帮帮我。代码是:

Intent intent = new Intent();  intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT); 
startActivityForResult(Intent.createChooser(intent,"Select Picture"),SELECT_PICTURE);

1 个答案:

答案 0 :(得分:0)

按钮点击事件,调用此功能

    private void showImageChooser(){
        Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        pickIntent.setType("image/*");
        Intent chooserIntent = Intent.createChooser(pickIntent, "Select Image");
        startActivityForResult(chooserIntent, 500);
    }

OnActivityResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == 500 && resultCode == RESULT_OK && data != null){
        imageFile = data.getData();
        Glide.with(getApplicationContext()).load(imageFile).into(profilePic);
    }
}

请使用ImageView代替profilePic。