Android Gallery Image Picker - Androhub

Gallery ImagePicker

Android Gallery Image Picker

Android supports seamless integration of applications and content providers. Lot many things are handy provided to developers, which helps in reducing the code and consumes less time to integrate different features to app.

For example, if we have an application that allow user to change his profile picture. In this requirement user will be welling to browse his mobile gallery and pick a picture of his own to set his profile picture. Integrating this feature in traditional operating system, need lot of effort write code to integrate file browser. But in android we can use Intent.ACTION_PICK action to get this feature integrated .

Example

In this tutorial, we are going to learn how to pick an Image from Gallery and display in our app.

Video Demo

1. Create a new project in Eclipse by navigating to File ⇒ New Android ⇒ Application Project and fill required details. By default my activity is MainActivity.java.

2. First create a layout in xml file naming activity_main.xml that contains Button for selecting image , and ImageView to display image and two TextViews for displaying URI path and original path of image.

3. In above layout you had seen i had set background to FrameLayout creating a border to ImageView for this we have to create an xml file under drawable directory naming border.xml.

4. Now there are only three main steps by which we can achieve our goal :

Step 1: Starting Intent

  • ACTION_PICK – Action Pick will open the picker in Android device.
  • setType(“image/*”) – This will set the type of picker i.e. all image type. So action_pick will open gallery.

Step 2: Receiving the gallery response
The selected image will result in onActivityforResult where we get the image URI and we convert the URI into real path via below method :

Step 3: After receiving response we have to compress the image
We have to compress and scale the image because large images can cause Exceptions so better to avoid exception. The below method is going to do this task and return a bitmap:

5. Finally  come to your MainActivity.java and add the following code that we had discusses in above point.

6. Now, you are all done, run your app and you will get the output as shown in video.

Thanks. :)

 

Post comment

Your email address will not be published. Required fields are marked *