How to Select an Image from Gallery in Android?
Selecting an image from a gallery in Android is required when the user has to upload or set their image as a profile picture or the user wants to send a pic to the other. So in this article, it’s been discussed step by step how to select an image from the gallery and preview the selected image. Have a look at the following image what’s been discussed further in this article.
Steps to implement image selection from the gallery
Step 1: Create an empty activity project
- Create an empty activity Android Studio Project. And select Java as the programming language.
- Refer to Android | How to Create/Start a New Project in Android Studio? to know how to create an empty activity Android Studio Project.
Step 2: Working with the activity_main.xml
- The main layout of the application includes one button to open the image selector, and one Image View to preview the selected image from the gallery.
- To implement the layout of the application, invoke the following code inside the activity_main.xml file.
Step 3: Working with the MainActivity.java file
- In this case, the imageChooser is triggered with the intent of the type “image” and action as ACTION_GET_CONTENT.
- Invoke the following code to implement the same. Comments are added for better understanding.
Output: Run on Emulator
Alternative Code: In case: startActivityForResult is deprecated