Get Image from the Gallery and Show in ImageView

前端 未结 2 662
夕颜
夕颜 2020-11-30 05:49

I need to get an image from the gallery on a button click and show it into the imageview.

I am doing it in the following way:

    btn_image_button.se         


        
2条回答
  •  醉酒成梦
    2020-11-30 05:57

    I use this code: This code used to start gallery activity.

    Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
    photoPickerIntent.setType("image/*");
    startActivityForResult(photoPickerIntent, GALLERY_REQUEST);
    

    And get the result in:

    @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if(resultCode == Activity.RESULT_OK)
            switch (requestCode){
                case GALLERY_REQUEST:
                    Uri selectedImage = data.getData();
                    try {
                        Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
                        carImage.setImageBitmap(bitmap);
                    } catch (IOException e) {
                        Log.i("TAG", "Some exception " + e);
                    }
                    break;
            }
        }
    

    And don't forget to declare permission in AndroidManifest.

    
    

提交回复
热议问题