How to receive text and images data from other applications in android

In applications like facebook,watsapp etc we can share text and images from other application . While clicking on share button there a popup will arise with a list of application which allows sharing . In this tutorial we will learn how receive text and images which is shared from other application by showing our application inside share dialog.

Contents

  • How to receive text from other applications
  • How to receive image from other applications
  • How to receive multiple images from other application

How to receive text from other applications

For receiving a text from other application ,first step is to setup our application to be displayed in the share intent dialog when some application want to share a text. For doing that first you need to have the following code in your AndroidManifest.xml inside the activity tag of the activity which the incoming text to be used.

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

So the above code will help you to show your application on share dialog when some application needs sharing text. Now we need to do some coding to handle the string received. Here i will write the code to get the string and setting it into a textview.

if (Intent.ACTION_SEND.equals(action) && type != null) {
    if ("text/plain".equals(type)) {

        String text_received = intent.getStringExtra(Intent.EXTRA_TEXT);
        if (text_received != null) {
           ed_email.setText(text_received);
        }
    } 
    }
}

How to receive image from other applications

For receiving a image from other application ,first step is to setup our application to be displayed in the share intent dialog when some application want to share a image. For doing that first you need to have the following code in your AndroidManifest.xml inside the activity tag of the activity which the incoming image to be used.

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

So the above code will help you to show your application on share dialog when some application needs sharing image. Now we need to do some coding to handle the image received. Here i will write the code to get the image and setting it into a imageview.

if (Intent.ACTION_SEND.equals(action) && type != null) {
    if (type.startsWith("image/")) {

        Uri image_received = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
        if (image_received != null) {
            im_incoming.setImageURI(image_received);
        }
    }
}

How to receive multiple images from other applications

For receiving multiple image from other application ,first step is to setup our application to be displayed in the share intent dialog when some application want to share multiple images. For doing that first you need to have the following code in your AndroidManifest.xml inside the activity tag of the activity which the incoming multiple images to be used.

<intent-filter>
    <action android:name="android.intent.action.SEND_MULTIPLE" />
    <category android:name="android.intent.category.DEFAULT" />
    <data android:mimeType="image/*" />
</intent-filter>

So the above code will help you to show your application on share dialog when some application needs sharing multiple images. Following code will help you to get all images which is shared from other application in your activity file . The images are received as an array list of uri. You can do a for loop iteration and allocate each images to imageview or you can even dynamically create imageview and add it your layout file.

if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
 if (type.startsWith("image/")) {</pre>
ArrayList<Uri> received_images= intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (received_images!= null) {
//you can create imageview dynamically here and set image uris
}

}
}

About the author

Hi guys, i am the author of codesfor. I am a B.Tech graduate currently working as an App developer. Apart from job i am a blogger and a freelancer.

Add a Comment

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