How to send Data between two individual applications in Android?

By | April 1, 2013

We can send data between two individual applications in Android if the receiving application is configured to received that type of data.
For example if an application is to receive Image data then it’s manifest should contain the ImageFilter as Shown below

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

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

                <data android:mimeType="image/*" />
            </intent-filter>

For example ac activity in the receiving application is named “Receiver” then its entry in the AndroidManifest.xml should be like this.

 <activity
            android:name="com.example.receivingapplication.Receiver"
            android:label="@string/app_name" >             
            <intent-filter>
                <action android:name="android.intent.action.SEND" />

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

                <data android:mimeType="image/*" />
            </intent-filter>
        </activity>

This line means that the Receiver application can receive any image type data.

Now in this post I have two application each have an activity. One is the Sending application and other is the receiving application.

At first I will explain what am I doing in “Sender application”

SENDING APPLICATION STARTS

My Only activity in the SenderApplication will have a layout like this

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="Send Text" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignRight="@+id/textView1"
        android:layout_below="@+id/textView1"
        android:text="Send Image" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView1"
        android:layout_alignLeft="@+id/button1"
        android:layout_marginBottom="68dp"
        android:src="@drawable/ic_launcher" />

</LinearLayout>

My Activity will be looking like this

package com.example.sendingapplication;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class SendingActivity extends Activity {

	private static final int SELECT_PICTURE = 1;
	private String selectedImagePath;
	private ImageView img;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		img = (ImageView) findViewById(R.id.imageView1);
		Button send = (Button) findViewById(R.id.button1);
		send.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent sendIntent = new Intent();
				sendIntent.setAction(Intent.ACTION_SEND);
				sendIntent.putExtra(Intent.EXTRA_TEXT,
						"This is my text to send.");
				sendIntent.putExtra("MyKey",
						"This is second my text to send using my key.");
				sendIntent.setType("text/plain");
				startActivity(Intent.createChooser(sendIntent, "Send"));
			}
		});

		Button sendImage = (Button) findViewById(R.id.button2);
		sendImage.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				Intent intent = new Intent();
				intent.setType("image/*");
				intent.setAction(Intent.ACTION_GET_CONTENT);
				startActivityForResult(
						Intent.createChooser(intent, "Select Picture"),
						SELECT_PICTURE);
			}
		});

	}

	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (resultCode == RESULT_OK) {
			if (requestCode == SELECT_PICTURE) {
				Uri selectedImageUri = data.getData();
				selectedImagePath = getPath(selectedImageUri);
				System.out.println("Image Path : " + selectedImagePath);
				img.setImageURI(selectedImageUri);
				sendImage(selectedImageUri);
			}
		}
	}

	void sendImage(Uri selectedImageUri) {
		Intent shareIntent = new Intent();
		shareIntent.setAction(Intent.ACTION_SEND);
		shareIntent.putExtra(Intent.EXTRA_STREAM, selectedImageUri);
		shareIntent.setType("image/*");
		startActivity(Intent.createChooser(shareIntent, "Send Image"));
	}

	public String getPath(Uri uri) {
		String[] projection = { MediaStore.Images.Media.DATA };
		Cursor cursor = managedQuery(uri, projection, null, null, null);
		int column_index = cursor
				.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
		cursor.moveToFirst();
		return cursor.getString(column_index);
	}

}

Note that for sharing data we have to do nothing for the Sending Application.

Now Our Sending application is Over

RECEIVING APPLICATION STARTS.

This is my only activity in the Receiving application.

package com.example.receivingapplication;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class ReceivingActivity extends Activity {

	private Bitmap bitmap;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		LinearLayout lin = new LinearLayout(this);

		Intent intent = getIntent();
		String action = intent.getAction();
		String type = intent.getType();
		TextView tv = new TextView(this);
		lin.addView(tv);
		lin.setOrientation(LinearLayout.VERTICAL);
		if (Intent.ACTION_SEND.equals(action) && type != null) {
			if ("text/plain".equals(type)) {

				tv.setText(intent.getExtras().getString(Intent.EXTRA_TEXT));
				tv.append("\n" + (intent.getExtras().getString("MyKey")));

			} else if (type.startsWith("image/")) {
				if (bitmap != null) {
					bitmap.recycle();
				}
				ImageView img = new ImageView(this);
				Bundle bundle = intent.getExtras();
				Uri uri = (Uri) bundle.get(Intent.EXTRA_STREAM);
				img.setImageURI(uri);
				lin.addView(img);
		}

		} else {
			// Handle other intents, such as being started from the home screen
		}
		setContentView(lin);
	}
}

This is the AndroidManifest for the Receiving application.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.receivingapplication"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.receivingapplication.ReceivingActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />

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

                <data android:mimeType="text/plain" />
            </intent-filter>
            <intent-filter>
                <action android:name="android.intent.action.SEND" />

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

                <data android:mimeType="image/*" />
            </intent-filter>
        </activity>
    </application>

</manifest>

WATCH CAREFULLY THE INTENT FILTERS IN THE MANIFEST FOR THE RECEIVING ACTIVITY.
YOU CAN HAVE PLAIN TEXT, IMAGE OR MULTIPLE IMAGES SEND TO IT. I HAVE NOT WRITTEN CODE FOR SENDING MULTIPLE IMAGES NOW, IT WILL BE COMING IN THE NEXT UPDATE.

Website Pin Facebook Twitter Myspace Friendfeed Technorati del.icio.us Digg Google StumbleUpon Premium Responsive

Leave a Reply

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