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.
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:
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:
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 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.
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 firstname.lastname@example.org, via their blog at androidbook.blogspot.com, and on Twitter @androidwireless.
Need More Help Writing Android Apps? Check out our Latest Books and Resources!
Subscribe below and we’ll send you a weekly email summary of all new Code tutorials. Never miss out on learning about the next big thing.Update me weekly
Envato Tuts+ tutorials are translated into other languages by our community members—you can be involved too!Translate this post