Android Series: Utilizing Camera 'Share via.." option

Android Series: Utilizing Camera ‘Share via..” option

If you have ever build an android app which required you to use a camera, you should consider a different flow in your application, starting not from your app itself but from the built in android Camera application.
Let’s say you want to post a photo on twitter or facebook. In you standard flow you would open the application XXX, choose the options to take the photo and finally go to the activity responsible for sending that photo.
There is another way to do it, user chooses to open internal andorid Camera application, takes the photo and then chooses ‘Share via..’ option menu.
This is how to include your activity for handling image taken by the external Camera app in the ‘Share via ‘ option list.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
     package="com.softwarepassion.androcamera"
     android:versionCode="1"
     android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".AndroCamera"
                 android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".CamSender"
                 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>
    </application>
</manifest>

Assuming AndroCamera is the main activity, where a standard application flow starts, and CamSender activity is responsible for manipulating/sending the photo only.
To get the data in the activity responsible for handling received image you should call getExtras(..) and retrieve the data stream

package com.softwarepassion.androcamera;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class CamSender extends Activity {

    private Uri imageUri;
    private ImageView imageView;
   
    @Override
    public void onCreate(Bundle state) {
        super.onCreate(state);
        setContentView(R.layout.camsender);
        imageView = (ImageView)findViewById(R.id.img);
       
        imageUri = (Uri) getIntent().getExtras().get(Intent.EXTRA_STREAM);
        imageView.setImageURI(imageUri);
    }
   
}

One response on “Android Series: Utilizing Camera ‘Share via..” option

  1. Zulfiqar June 7, 2011 at 9:16 am

    Dear, when we register for intentFilter, there are plenty of options already added., what can we do to make our application option to come first in the options menu list generated by sytem for shareimage menu…
    can we set any kind of priority to do this

Leave a Reply