Good day.
How can I, by pressing a button, call up a gallery of a smartphone, can I select an image and use it as a button background and not only?

Button up to:
Button up

Button after:
Button after

I decided:
Using Intent.ACTION_PICK and Uri solved my problem

 static final int GALLERY_REQUEST = 1; imageButton = (Button) rootView.findViewById(R.id.add_image); imageButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent photoPickerIntent = new Intent(Intent.ACTION_PICK); photoPickerIntent.setType("image/*"); startActivityForResult(photoPickerIntent, GALLERY_REQUEST); } }); ... @Override public void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { super.onActivityResult(requestCode, resultCode, imageReturnedIntent); switch (requestCode) { case GALLERY_REQUEST: if (resultCode == RESULT_OK) { try { final Uri imageUri = imageReturnedIntent.getData(); final InputStream imageStream = getActivity().getContentResolver().openInputStream(imageUri); final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream); Drawable d = new BitmapDrawable(getResources(), selectedImage); imageButton.setBackground(d); } catch (FileNotFoundException e) { e.printStackTrace(); } } } } 
  • one
    Have you tried to search in Google? what exactly does not work? Here, people help solve problems, not just write the code for me . - Nikotin N
  • The question is too general and as the answer requires you to write a whole program and solve many questions, and not to solve a specific problem. Reissue the question so that it contains a specific problem that you cannot solve (I can’t call the gallery by clicking the button or how to get the image selected in the gallery or how to set the button background) - pavlofff

1 answer 1

In the stupid version, which is googled in 3 seconds:

 Intent intent = new Intent(); intent.setAction(android.content.Intent.ACTION_VIEW); intent.setType("image/*"); intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); startActivity(intent); 

In more modern versions there are other Intentions, but for starters, this is enough for you.

  • I think the author needs onActivityResult implementation and not just going to the gallery)) - Flippy