Search code examples
androidfacebookintentfilter

Determine type of content received via Intent Filter


I am working on an app which I want to register in the "Share" list. That part is already taken care of using the following tags in the manifest file:

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

After looking at a few example on SO and using bits an pieces from each relevant question, I have put together a simple (and yet clumsy) piece of code which, as of now, receives an image shared from the gallery. I have also been able to successfully upload that received image to Facebook.

My question is, I want my App to share a variety of content from various apps. For example, a tweet from Twitter, a URL from the browser, etc. How do I determine the exact nature of the data / content shared from another app to my app? I can process the received data appropriately. But I am stumped as to how I can figure out what content was shared.

The code I have put together for sharing an image is:

Note: I am currently running this piece of code in the onCreate() method without any error checking. I will put them in place when I have a clue as to the question in concern.

ImageView imgSelectedPhoto = (ImageView)findViewById(R.id.imgDisplaySelectedImage);

Uri imageURI = (Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);
Bitmap bitmap = null;
try {
    bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageURI));
        try {
        Utility.scaleImage(getApplicationContext(), imageURI);
        imgSelectedPhoto.setImageBitmap(bitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    } catch (FileNotFoundException e) {
        e.printStackTrace();
}

Any help / advice / suggestion is much appreciated.


Solution

  • You can get MIME type of data in intent received by your application as:

    String type = getIntent().getType();