Advertisement
Android SDK

Android SDK Quick Tip: Sending Pictures the Easy Way

by

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

Related Posts
  • Code
    Android SDK
    Common Entry Points for Android Applications5d7z4 preview image@2x
    In this tutorial, we will provide an overview of common entry points for Android applications. These entry points will focus on ensuring that your application maximizes its utility to the user.Read More…
  • Code
    Android SDK
    Using New Relic to Monitor Your Android AppGetting started new relic retina preview2
    In the last two years, New Relic has focused hard on building out a solution for monitoring the performance of mobile apps. In this tutorial, we will look at how you can start using New Relic to monitor the performance of an Android application.Read More…
  • Computer Skills
    Electronics
    How to Clone Raspberry Pi SD Cards Using the Command Line in OS XClonesd400
    The Raspberry Pi runs from an operating system stored on a Secure Digital (SD) card and many different operating systems may be employed. Storage is relatively inexpensive, can be created (flashed), recreated, written to and overwritten with ease. On the one hand, this is an advantage of the Pi. On the other, the experimental nature of the Pi means more time flashing SD cards. This tutorial shows you how to use a Mac to clone any Raspberry Pi SD card which is particularly useful when you have your OS set up just as you want it.Read More…
  • Code
    News
    Android 4.4 KitKat: What's NewAe2b1 preview image@2x
    Google released its latest version of Android on October 31, 2013. The new release, version 4.4, is nicknamed KitKat. The release came as a surprise to many as Key Lime Pie and 5.0 had been linked to this update for quite some time. KitKat, however, doesn't disappoint as it's packed with features and updates. In this article, I'll give you an overview overview of what's new in KitKat.Read More…
  • Computer Skills
    Electronics
    How to Install NOOBS on a Raspberry Pi With a MacNoobs400
    If you are new to Raspberry Pi and want to get up and running without too much fuss, then NOOBS is probably going to be one of the best places for you to start. NOOBS stands for New Out Of The Box Software and it’s intention is to make the start of your Raspberry Pi adventure somewhat less daunting than it otherwise might be, especially if you are unfamiliar with the command line interface, or CLI.Read More…
  • Computer Skills
    Electronics
    How to Flash an SD Card for Raspberry PiRpisd400
    The Raspberry Pi is an incredible little computer made up of a components and connectors on an unfeasibly small printed circuit board that is approximately the dimensions of a credit card. Depending upon your intended use, your Rasberry Pi may have a keyboard, monitor and mouse added. Regardless of your intended use, you are going to need an SD (Secure Digital) card on which to install an operating system. This tutorial describes how to install an operating system onto an SD card for use in your Raspberry Pi.Read More…