There is a fragment, there is an ImageButton. When I click on the button, the gallery should open, where I can select an image, then this image is loaded into the ImageButton.
How to implement it?
When clicking on ImagButton in the fragment, we write the following code:
imageButtonTest.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, GALLERY_REQUEST); } }); Next, in the activity, we override the onActivityResult () method and write:
@Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if(requestCode == GALLERY_REQUEST){ Uri fileUri = data.getData(); try { Bitmap photo = MediaStore.Images.Media.getBitmap(getContext().getContentResolver(),fileUri); imageButtonTest.setImageBitmap(photo); } catch (IOException e) { e.printStackTrace(); } } Source: https://ru.stackoverflow.com/questions/637713/
All Articles