Android SDK Quick Tip: Sending Pictures the Easy Way

Android SDK Quick Tip: Sending Pictures the Easy Way

Tutorial Details
  • Technology: Eclipse + Android SDK
  • Difficulty: Beginner
  • Completion Time: 10 - 15 Minutes

This quick tip shows you how to configure and send a picture to someone (for example, an MMS message). You will achieve this by creating and configuring the appropriate Intent within an application’s Activity, enabling the user to send the picture using their picture messaging application of choice.

Step 1: Creating an Android Application

Begin by creating an Android project. Once you have a project set up and the application running, decide under what circumstances you want to send picture content. Will this occur when a Button is pressed? Implement the necessary Button control, including any click handling. Once you have completed this task, you have a place to drop in the code to send a picture somewhere. Now you are ready to proceed with this quick tip.

The code for this quick tip is available as an open source project.

You will need to make sure you’ve got at least one working messaging application installed (like Messaging). This is best done using a real Android device, as opposed to the emulator.

Warning: Sending MMS messages may incur fees, depending on your phone plan.

Step 2: Creating the Intent

Mail applications can be launched to send a message with the following Intent: android.content.Intent.ACTION_SEND. Begin by creating this Intent within your Button click handler:

Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);

This is a very generic Intent type. Basically, it tells the Android system that your application wants to send some sort of data somewhere—the ACTION_SEND Intent can be used to send different types of messages such as text messages (SMS) and picture messages (MMS), emails, etc. One common use of the ACTION_SEND Intent is to send plain text messages like emails, as we discussed in a previous Quick Tip.

Step 3: Saving a Picture File to the Device

The graphic file must be accessible to both the applications—your application and the messaging application. Since you cannot be sure which application will be sending the picture, you should make sure the graphic is stored in a public place. One of the simplest places to store data is on external storage (the SD card) in the public directory. For simplicity’s sake, we’ll simply use the Browser application to save an image to the SD card and then access it for use within the Intent code.

To save an image to the SD card, launch the Browser application and find an image. For this example, we downloaded the q.jpeg graphic from our book website. To download an image in the Browser, long-press on the image and choose Save Image. Note the name of the graphic in the Notification bar.

Step 1 Picture

In practice, your app can create or download an image however it likes. Images are best stored in a common place, though, so any app can access them. For instance, the Browser app does this and we’re leveraging that here.

Step 4: Configuring the Intent Type

In order to use this ACTION_SEND Intent to send a picture, you will need to add some additional configuration information. Specifically, you need to set the type of the Intent using the setType() method to the appropriate MIME type of the graphic file. For example, to send a JPEG image, you would set the type (lowercase) of the Intent as follows:

picMessageIntent.setType("image/jpeg");

Step 5: Generating a Picture URI

You know the filename of the picture. You can use this information to generate a Uniform Resource Identifier (Uri) that describes the location of the file and pass this Uri to the Intent. In this case, the downloaded picture is the cover of our most recent book (q.jpeg). To supply the Intent with the Uri to the picture to include in the message, you must use the extra called Intent.EXTRA_STREAM.

But where is the picture? In order to generate the correct Uri, you can use the handy Environment class, which has a method to help identify the appropriate download directory in the public directory hierarchy of the SD card. The following code creates the Intent and configures the extras to include a picture called q.jpeg, which must exist in the public user downloads directory of the SD card:

Intent picMessageIntent = new Intent(android.content.Intent.ACTION_SEND);
picMessageIntent.setType("image/jpeg");
File downloadedPic =  new File(
    Environment.getExternalStoragePublicDirectory(
    Environment.DIRECTORY_DOWNLOADS),
    "q.jpeg");
picMessageIntent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(downloadedPic));

Step 6: Launching the Intent

When you launch this Intent, any applications that support sending will be able to handle the request. Simply call the startActivity() method, passing in your Intent:

startActivity(picMessageIntent);

The details of how the picture is handled depend upon the application chosen to handle the Intent. If the user chooses the Messaging application, the picture is converted into an MMS message. If the user chooses an email application, the picture will be sent as an attachment.

Step 2 Picture

Step 7: Handling When Multiple Applications Can Handle Picture Messages

Users can install any number of Android applications on their devices. Often, they install multiple messaging and image sharing applications. You can allow the user to choose which application to handle the Intent by supplying the startActivity() call with createChooser() method:

startActivity(Intent.createChooser(picMessageIntent, "Send your picture using:"));

As you can see, many popular applications can send pictures using the ACTION_SEND Intent.

Step 3 Picture

Conclusion

In this quick tip you have learned how to configure an Intent to send an picture message. Depending on which application handles the ACTION_SEND Intent, the message might be sent as an MMS message, an email with a picture attachment, or using some other application’s picture sending protocol. The user still has ultimate control and flexibility on whether or not to send the message and how it is sent.

About the Authors

Mobile developers Lauren Darcey and Shane Conder have coauthored several books on Android development: an in-depth programming book entitled Android Wireless Application Development and Sams TeachYourself Android Application Development in 24 Hours. When not writing, they spend their time developing mobile software at their company and providing consulting services. They can be reached at via email to androidwirelessdev+mt@gmail.com, via their blog at androidbook.blogspot.com, and on Twitter @androidwireless.

Need More Help Writing Android Apps? Check out our Latest Books and Resources!

Buy Android Wireless Application Development, 2nd Edition  Buy Sam's Teach Yourself Android Application Development in 24 Hours  Mamlambo code at Code Canyon

Note: Want to add some source code? Type <pre><code> before it and </code></pre> after it. Find out more
  • http://www.aafrin.com Aafrin

    simple but very informative tutorial… thanks for the guide…

  • ljubica

    I am new to android, I tried your guide and works great, only I wonder what will get if I choose to send the image through facebook. I had no android device, so I use android emulator for testing, but the emulator does not provide an option for sending via facebook. Greetings.

    • Aggie Warsito

      That was because no facebook apps installed on your android emulator. ^^

  • http://www.pedroteixeira.org Pedro Teixeira

    Hi,

    I have my image as a bitmap. How can I convert it to a file and send it? Instead of a file from SDCard? My pictures are stored on a server.. Thank you

    • Barry

      Hi Pedro.

      Did you ever work out how to do that? It is something id like to know how to do too.

      I’ll post a solution if / when I find one.

      Barry.

  • http://omglolz.info funny stuff

    Hello!
    I love your blogpost. This is interesting to read.
    I wonder why I didn’t know about this blog earlier.

    I will surely spread the word. Many of my Facebook friends will like this.

    Thanks and keep’em coming!
    :-)

  • Dano

    I get this error on step five:

    “DIRECTORY_DOWNLOADS cannot be resolved or is not a field”

    only option it suggests is to rename it.

    Dano.

    • Aaron

      I second this. I can fix it by switching to API 8 but my target is API 3, is there an alternate method that will work with API 3?

      Thanks

  • romanzah

    i was tried.
    but the application is eror.
    whats wrong with my app?

    import java.io.File;

    import android.app.Activity;
    import android.content.Intent;
    import android.net.Uri;
    import android.os.Bundle;
    import android.os.Environment;

    public class mainn extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    Intent picMessage= new Intent(android.content.Intent.ACTION_SEND);
    picMessage.setType(“image/jpeg”);
    File donlod= new File(Environment.getExternalStorageDirectory(),null );

    picMessage.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(donlod));

    startActivity(Intent.createChooser(picMessage, “ngirim pke ap?”));
    }
    }

  • http://www.engiguide.com ajaypatelaj

    Tanks for amazing artical….

    and i have problem is that k. where is SD in android desktop emulator …
    and from where can i give path of image file…
    each guy will get this error…please help me to solve it…

    • https://plus.google.com/111516293970666237598/posts PUSHPENDRA KUNTAL

      Ajay, have you found that where is sd in emulator..? if no then ask to me. I will tell you related to this.

  • http://www.engiguide.com engiguide

    i think its not working in emulator.. for me its not working

  • rakesh

    Thank u, This code helps me alot.

    But I need send the mail with text and image as signature in compose mail body.

    Note: Not as an attachement.

  • Nddo

    Hi frnz I wan to send my college log via SMS …….. Can anybody suggets me the .Net code for tat/..

  • http://surangi.blogspot.com sara

    Tanks for the amazing artical….

  • gouthami

    the application works great but i want only the email applications. Instead I’m getting even bluetooth messaging and so on… i just want only email applications like gmail, skype, yahoo,etc whatever i have in my phone

  • http://internet-radio-player.com/ internet radio

    Hello! I am a fresh owner of Android, and I do not know all its functions yet. That’s why your article was very helpful to me. I constantly exchange messages with my friends and relatives who are far away, and the option of sending pictures through the messages is definitely of much use.