Android SDK

Android SDK: Receiving Data from the Send Intent


On the Android platform, the Intent system allows users to share content between apps. You can send and receive multiple types of data on Android but in this tutorial we will implement receiving either a text String or an image, displaying the received content in the app's user interface!

Step 1: Start a New Android Project

Start by creating a new Android project in Eclipse. Choose "File", "New", "Project" - select "Android Application Project" and then click "Next". In the New Android Application window, enter your chosen Application, Project, and Package names and then choose "Next".

New Android Project

In the project configuration window, choose your options, letting Eclipse create an Activity for you. In the next screens, configure your launcher icon if appropriate, and then select "Blank Activity". In the Configure Activity screen, enter an Activity name and then choose "Finish".

Blank Activity

Eclipse should create Activity and layout files for your app.

Step 2: Edit the Project Manifest

In order for your app to appear in the chooser list presented when the user attempts to share data from another app, you need to alter the Project Manifest file. Open it in Eclipse and switch to the XML editor tab. Find the section containing your app's main Activity - you should see an Intent Filter element there already, indicating MAIN action and LAUNCHER category.

After the existing Intent Filter element, inside the main Activity element, add another Intent Filter:



This is where we define the Actions that can launch the app - the existing Intent Filter specifying the normal options for when the user launches from the homescreen or device application menu. By adding another Intent Filter element, we are specifying additional options for launching the app. Inside the new element, add an Action element specifying the SEND Intent Action:

<action android:name="android.intent.action.SEND" />

This tells Android that when other apps launch a SEND Intent this app may receive what is being shared. Now give the Intent Filter a category:

<category android:name="android.intent.category.DEFAULT" />

Next we need to indicate the MIME types the app is going to receive:

<data android:mimeType="image/*" />
<data android:mimeType="text/*" />

This indicates that the app will receive any text or image type. You can optionally specify particular text or image types. There are lots of other types you can receive, including audio, video, message, and application. If you need your app to receive multiple data items of one of your MIME types, you can include the SEND_MULTIPLE Action as well as/instead of the SEND Action alone.

Here is the "DataReceiver" app appearing in the share list for the Gallery application, through which various image types can be shared.

The App in the Share List

Step 3: Define the App Layout

Let's design the app to display the received text String or image. Open the main layout file and switch to the XML tab to edit the code. Replace any existing code in the layout file with the following Relative Layout outline:

<RelativeLayout xmlns:android=""
	tools:context=".DataReceiverActivity" >


Alter the "tools:context" attribute if you chose a different Activity class name. Inside the layout, add the following to display a received image:


We will use the ID to refer to the Image View in our Java Activity, updating it to display any image received from the SEND Intent. Add the specified String to your app's "res/values/strings.xml" file:

<string name="pic">Received Picture</string>

Back in the layout file, add a Text View to display any text Strings received via the SEND Intent:


Again, we have an ID attribute we can use in the Java code.

Step 4: Receive the Intent

Open your app's Activity class file. Your class will need the following import statements, some of which may already be present:

import android.os.Bundle;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

Eclipse should have created a basic onCreate method applying your layout. After this line, retrieve the Views we want to update with any received data:

//get the image view
ImageView picView = (ImageView)findViewById(;
//get the text view
TextView txtView = (TextView)findViewById(;

These use the ID attributes we included in the layout file. Now that we have a reference to the Views, we can update their appearance with data received from other applications. Within an Activity class, you can retrieve the Intent that was responsible for the Activity starting. Add the following line in your onCreate method:

//get the received intent
Intent receivedIntent = getIntent();

Now we can use this to also retrieve the Action that was used when starting the Activity:

//get the action
String receivedAction = receivedIntent.getAction();

This will let us determine whether the app has launched from the device menu or from the share list. If the app has been launched with the SEND Action, there will also be a MIME type indicated within the Intent that started it. Retrieve this as follows:

//find out what we are dealing with
String receivedType = receivedIntent.getType();

If the app was launched, for example, from the main device menu rather than from a share list, this will be null. If it was launched with the SEND Action, we will be able to use this to handle the received data, tailoring the app response to the type of data received.

Users can share text from a variety of applications, including the Web browser - if a user chooses to share a Web page, this data is contained in a text String, so our app will be able to receive it:

The Browser Menu

Here is the app listed in the browser share list:

The App in the Browser Share List

Step 5: Handle Alternative Actions

Now we have two possibilities: the app has been launched from the device in the default fashion and is not receiving any incoming data; the app has been launched to share content, in which case there is either a text String or image incoming. Add a conditional statement to onCreate to handle these two scenarios:

//make sure it's an action and type we can handle
	//content is being shared	    	
else if(receivedAction.equals(Intent.ACTION_MAIN)){
	//app has been launched directly, not from share list

If you look back at your Manifest file you will see that these are the Actions included in our two Intent Filter elements for the Activity. Let's complete the ACTION_MAIN else if statement first as it's simpler. In here you need to include whatever you want your app to do when nothing has been shared to it. For the purposes of this tutorial we will simply display a text String in the Text View:

txtView.setText("Nothing has been shared!");

Step 6: Determine MIME Type

Now to the ACTION_SEND if statement, in which we need to handle received data of the MIME types we listed in the Manifest. First add a conditional to handle the two types we listed, text and images:

	//handle sent text
else if(receivedType.startsWith("image/")){
	//handle sent image

If you set your app up to receive other types, simply add conditional statements here for each of them.

Step 7: Receive Shared Text Data

Let's receive text first. In the if statement for text data, first hide the Image View as we only need the Text View in this case:

//hide the other ui item

Now retrieve the String as an Extra from the Intent:

//get the received text
String receivedText = receivedIntent.getStringExtra(Intent.EXTRA_TEXT);

We use the getStringExtra method for text data, passing the EXTRA_TEXT Intent constant to receive any character sequence passed using the SEND Action. Now let's make sure we have some data and display it in the app UI:

//check we have a string
if (receivedText != null) {
	//set the text

You can of course carry out further processing on the text if you need to - this is the text URL shared from the browser:

Shared Text in the App UI

Step 8: Receive Shared Image Data

Now let's turn to the else if statement for image data. As before, first set the alternative UI item to disappear as we don't need it:

//hide the other ui item

Now we need to get the image URI, as this is what the Intent will be sharing:

//get the uri of the received image
Uri receivedUri = (Uri)receivedIntent.getParcelableExtra(Intent.EXTRA_STREAM);

This time we use the getParcelableExtra method - there are lots of other Intent methods for the different data types you can receive in your apps, including number types, arrays and lists. We pass the EXTRA_STREAM Intent constant this time, which is used to receive data other than text. Now we can use the image URI within the app - the sample code below simply displays it in the Image View but if you plan on receiving images in your own apps you should first resample the image data to avoid memory issues:

//check we have a uri
if (receivedUri != null) {
	//set the picture
	picView.setImageURI(receivedUri);//just for demonstration

Tip: For guidance on avoiding memory problems when importing images see the Android Developers guide: Displaying Bitmaps Efficiently

Here is the image shared from the Gallery app:

Shared Image in the App UI


That's our basic receiver app complete. This is of course just the bare bones of a functional app in which data is received from the SEND Action, but the principle of setting your app up to appear in the share list and handling the incoming content remains. In general, it's worth thinking of the possible ways in which users may want to launch your apps, as the main menu launcher is really just the beginning.

Related Posts
  • Code
    Android SDK
    Create a Music Player on Android: Project Setup0d63m preview image@2x
    The Android platform provides resources for handling media playback, which your apps can use to create an interface between the user and their music files. In this tutorial series, we will create a basic music player application for Android. The app will present a list of songs on the user device, so that the user can select songs to play. The app will also present controls for interacting with playback and will continue playing when the user moves away from the app, with a notification displayed while playback elapses.Read More…
  • Code
    Android SDK
    Using the Accelerometer on Android06lom preview image@2x
    In this tutorial, we are going to explore how to use the accelerometer, one of the many hardware sensors of modern smartphones, in an Android application. I'll explain what an accelerometer is and why it may be something you want to take advantage of in your Android applications.Read More…
  • Code
    Creating a Photo Tag Wall With Twilio Picture Messaging & PHPProcedural to oop php retina preview
    Twilio's recently announced Picture Messaging has vastly opened up what we can do with text messaging, now we can attach photos to our text messages and have them get used in different ways. In our case, we are going to build a Photo Tag Wall, which will contain photos linked to tags that will be displayed on a website.Read More…
  • Code
    Android SDK
    Android SDK: Next StepsAndroid preview@2x
    In this series, we've begun learning how to develop Android applications from scratch. We started exploring the development tools, got acquainted with the basic elements in an application project, looked at user interface design, interactivity, resources, and data, and we've also took a closer look at what happens when your application is running. What we've covered so far should put you in a good position to get started creating functional Android applications, but Android has a lot more to offer so the range of possibilities is virtually endless. You may therefore struggle to choose what to learn next. In this part, we'll wrap up the series by pointing out some possible directions for future learning. After this, the final part will be a quiz on what we covered throughout the series.Read More…
  • Code
    Android SDK
    Android SDK: Common Android ComponentsAndroid preview@2x
    In this series we are learning the essential features of Android development that you need to know to start building apps. So far, we've looked at the structure and typical elements in an Android app, including user interface elements and data storage. You can use what we covered already to start creating your own apps. But before you do, we will run through some common Android components in this tutorial, then have a quick look at the SDK samples in the next tutorial.Read More…
  • Code
    Android SDK
    Android SDK: Project ManifestAndroid preview@2x
    In this series of tutorials we are learning about Android development from scratch and getting acquainted with the basic concepts and practices you need to know in order to start working on the platform. So far we have looked at various parts of an Android project, including its resources. In this tutorial, we will explore the project manifest file.Read More…