Intent - Get image from gallery

An Intent is a messaging object to create communication between activities. You can use the Intent to start an activity, start a service, or deliver a broadcast. Android supports explicit and implicit intents.

Explicit Intents

An explicit intent is used connected internal components of your application, suppose if you wants to connect one activity to another activity. Explicit intents explicitly define the component which should be called by the Android system, by using the Java class as identifier. An intent contains certain header data, e.g., the desired action, the type, etc. You can add data to the intent using putExtra() method.

Intent intent=new Intent(MainActivity.this, SecondActivity.class);
intent.setAction (MainActvity.ACC_ACTION);

The receiving component can access this information via the getXxxExtra() on the Intent object.

Intent intent = getIntent();
if (intent != null && intent.getAction()!=null) {
    if (intent.getAction().equals (Mainactivity.ACC_ACTION)) {
       String user_info = intent.getStringExtra("userinfo");

You also can add data to the intent using a bundle object. You call putExtras() method to add the bundle object to the intent.

Intent intent = new Intent(this, DetailNews.class);
Bundle extras = new Bundle();
extras.putString("info_id", item.getId());
extras.putString("title", item.getTitle());

The receiving component can access this information via the getExtras () on the Intent object.

Bundle extras = getIntent().getExtras();
if (extras != null) {
   String news_id = extras.getString("info_id");
   String title = extras.getString("title");

Implicit Intents

Implicit intents specify the action which should be performed. Components that are able to get the intents must be registered for the specific action and data type.

String path= Environment. getExternalStorageDirectory() + "/download/myfile.pdf";
File file = new File(path);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType (Uri.fromFile(file),"application/pdf");
Intent intent = Intent.createChooser(target,  "Open File");

Receiving an implicit intent

To receive implicit intents, in your app, you need to declare one or more intent filters for each of your app components with an <intent-filter>element in your manifest file.
Each intent filter is defined by an <intent-filter> element in the app's manifest file, nested in the corresponding app component (such as an <activity> element). Inside the <intent-filter>, you can specify the type of intents to accept using one or more of these three elements:
   Declares the intent action accepted, in the name attribute. The value must be the literal string value of an action.
    Declares the type of data accepted, using one or more attributes that specify various aspects of the data URI and MIME type.
   Declares the intent category accepted, in the name attribute. The value must be the literal string value of an action.

<activity android:name="ShareActivity">
<action android:name= "android.intent.action.SEND"/>
<category android:name=  "android.intent.category.DEFAULT"/>
<data android:mimeType="text/plain"/>

In this example, you learn to display your app in the list of apps chooser and display a selected image from gallery.
- Now, create a new project in Android Studio. Name the project as ImplicitIntentExample.
- Add the following intent filter to your MainActivity in AndroidManifest.xml file. To receive an image sent from gallery, the action you need to specify here is SEND and the category is DEFAULT. The mine type of the data to receive is image/*. There are lots of other types you can receive, including audio, video, message, and application
<action android:name= "android.intent.action.SEND"/>
<category android:name= "android.intent.category.DEFAULT"/>
<data android:mimeType="image/*"/>

- In the activity_main.xml file, you add an ImageView to display an image shared from gallery.
<?xml version="1.0" encoding="utf-8"?>
xmlns:android= " /apk/res/android"
xmlns:tools= ""
android:paddingLeft= "@dimen/activity_horizontal_margin"
android:paddingRight= "@dimen/activity_horizontal_margin"
android:paddingTop= "@dimen/activity_vertical_margin"
android:paddingBottom= "@dimen/activity_vertical_margin"
tools:context=  "com.example.dara. implicitintentexample. MainActivity">


- In the onCeate() method of the MainActivity class, the code to receive the shared image and display it in the imageview can be written as shown below.

protected void onCreate(Bundle savedInstanceState) {
   ImageView imgView=(ImageView) findViewById(;
   Intent intent=getIntent();
   String action=intent.getAction();
   String type=intent.getType();
   if(Intent.ACTION_SEND.equals(action) && type.contains("image")) {
      Uri file_uri = (Uri) intent.getParcelableExtra (Intent.EXTRA_STREAM);

Now, let’s run the app. Then go to gallery and share an image. You will see your app is now on the list of apps chooser. Select your app to view the shared image.

intent gallery

Posted by: Dara | post date: 12-22-2014 | Subject: Android Apps Development

write programming tip

This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.