Image Picker Using Intent.ACTION_PICK in Android

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 in not more than five minutes.

What is intent in android?

Intent is an abstract description of an operation to be performed. It can be used with startActivity to launch an Activity, broadcastIntent to send it to any interested BroadcastReceiver components, and startService(Intent) or bindService(Intent, ServiceConnection, int) to communicate with a background Service.

Intent provides a facility for performing late run-time binding between the codes in different applications. Its most significant use is in the launching of activities, where it can be thought of as the glue between activities. It is basically a passive data structure holding an abstract description of an action to be performed.

Writing image picker using Intent.ACTION_PICK in android

Intent.ACTION_PICK action is in buit in android and helps to pick an image item from a data source. We just need to provide the URI of the provider. Almost all core android applications (eg. Messaging, Gallery, Contacts etc) provide this facility. All you need is to set the intent action and the data.

action – tells what to perform, such as ACTION_VIEW, ACTION_EDIT, ACTION_MAIN, etc.data– data needed to operate the action. It is expressed as uri. Example such as a person record in the contacts database. Some examples of action/data pairs are:

ACTION_VIEW content://contacts/people/1 — Display information about the person whose identifier is “1”.
ACTION_DIAL content://contacts/people/1 — Display the phone dialer with the person filled in.
ACTION_VIEW tel:123 — Display the phone dialer with the given number filled in. Note how the VIEW action does what what is considered the most reasonable thing for a particular URI.
ACTION_DIAL tel:123 — Display the phone dialer with the given number filled in.

There are also some more additional attributes. Visit the below llink for more details

As you can see the code snippet used is pretty simple. Here in this example, I am handling button click event and their Image picker using Intent.ACTION_PICK has been called. setType() method is used to define the file filter criteria. This will invoke the default gallery and user will be able to select an image.

Once image is selected, the result will be back to our main activity, and the result will be returned to onActivityResult() method. Here we are receiving the selected image Uri. Once we have the uri, we can convert them to Bitmap and then display it on ImageView. Below coded snippet is used to get the image from the Uri and convert into bitmap.

Output of the above code is below

A blogger, a bit of tech freak and a software developer. He is a thought leader in the fusion of design and mobile technologies. He is the author of Xamarin Mobile Application Development for Android Book (goo.gl/qUZ0XV3), DZone MVB and founder of stacktips.com.

Discussions

Please note: We reserve the right to delete comments that contains snarky remarks, offensive or off-topic. To know more read our comments policy.

500

10Comment threads

2Thread replies

0Followers

Most reacted comment

Hottest comment thread

500

newestoldestmost voted

keerthanaR

i am new to the android, can anyone explain the below code: already case is receiving the cmd from SELECT_PHOTO: , then why we are using if case? what will be in (int requestCode, int resultCode, Intent imageReturnedIntent) ; what this will do? super.onActivityResult(requestCode, resultCode, imageReturnedIntent); please explain step by… Read more »