The Android for .NET Developers Series
Part 1 Starting with this article, I'll discuss what you need to know to approach Android programming without any aid from your .NET expertise.
Part 2 In this article, we'll go through an Android application that accepts input from the user and handles user's clicking.
Part 3 In this article, you will learn how to build the user interface.
Part 4 In this article, I'll be delving deep into menus and dialog boxes in Android for .NET, and discuss a few very common (and frequently used) types of menus and dialogs.
Part 5 In this article, Dino Esposito focuses on the options that the Android SDK offers for local storage.
Part 6 In this article, Dino Esposito shows how to build settings dialog boxes using a built-in feature of Android for .NET.
Part 7 In this article, I'll dissect the code of a realistic application - a waterpolo score manager - to show how to save and resume the status of a game using both the internal storage and SD card.
Part 8 In this article, I'll focus on the execution of common tasks from within an Android application. I'll build the skeleton of an application that takes a photo and emails to the specified address. For both tasks I'll use native Android components.
Part 9 In this article, I'll discuss how to arrange an Android view where the dominant element is the list. I'll start with a plain list of strings and then improve up to populating a listview with downloaded content arranged using a custom layout.
Part 10 In this article, I'll discuss the features and capabilities required by an Android application to talk with Twitter. In particular, I'll focus on authentication and updates.
Introduction
Nearly any GUI platform has some reusable pieces of code that applications can use to implement common tasks in the same guise as the system. In Windows, you have common dialogs to select and save a file, pick up a color or a font. Mobile platforms are no exception. Reusable system code are applets to send an email, take a picture, select a contact, send a text message, and more. The way in which this system code is invoked, and the way in which you interact with it may be different in the various mobile operating systems. Also different is the extent to which you can interact with this system code. But this code exists and is fairly use to use and incorporate in user applications.
In this article, I'll focus on the execution of common tasks from within an Android application. I'll build the skeleton of an application that takes a photo and emails to the specified address. For both tasks I'll use native Android components.
Android Intents
What in Windows Phone 7 is a chooser and in iPhone a special controller (e.g., the UIImagePickerController for using the camera), in Android takes the name of an intent. Personally, I would see an Android intent closer to a Windows Phone chooser or launcher than to an iPhone ad hoc controller. At any rate, the definition that the Android SDK offers for an intent is an abstract description of an operation to be performed. An intent is associated with an action and some data. The action is a string (a system mnemonic string) that describes a supported action; the data is any data associated with the execution of the action. For example, the camera intent returns the captured picture as its output data and it may optionally take the name of the file to save the picture to as input data.
In Android, you use intents for executing a variety of system tasks such as sending a text message, placing a phone call or, as this article illustrates, capturing a picture and sending it via email.
The Basic Application
The following listing shows the foundation of a simple Android application that shows off a big button to trigger the camera and email program in sequence. The user interface also contains an image view for previewing the captured image.
Figure 1 shows the user interface of the sample application with the screen in landscape mode.
Figure 1: The sample application up and running and waiting to capture an image

The idea is that the user clicks the Shoot button, deals with the built-in camera application, takes a photo and then displays it in the preview box. At the same time, the email native application will kick in to propose a pre-arranged email ready to be sent. Let's take a look at the code in the click handler for the shoot-n-send button.
Launching the Camera Intent
In Android, you have two ways for working with the device camera. You can choose to operate at a very low level and use the camera API or you can rely on the services of the built-in application. In the latter case, you need to get an instance of the corresponding intent, as shown below.
Notice that you don't need to declare any special capabilities in the application's manifest if you simply use the camera intent. Instead, if you use the API directly then it is required that you add the following permission (and feature) to the manifest.
To start the intent and display the native application you just call startActivity and pass the intent instance you've got. The startActivity method is defined on the Activity class and is then globally available (or a reference to the runtime context must be passed around). All in all, just calling the default camera application is kind of pointless; you need to establish a bit of interaction to make things more interesting. At the very minimum, you want to instruct the application to save the taken picture to a particular location and with a given name.
The putExtra method on the Intent class adds the specified URI as the output file name. The URI is just a value added to the data collection of the intent; the code associated with it, though, adds a role to the data and contains instructions for a particular intent. The file name points to a file in path within the range of the SD card. Note that all folders in the path must exist otherwise no file will ever be created.
That would be enough to take a picture and save it to a given location. What if you want to display a preview and maybe send an email later? In this case, you need to claim a notification for when the camera intent has done its job - that is, after the user has accepted the picture through the standard Done/Cancel pair of buttons. You use the method startActivity to fire-and-forget the intent; you use startActivityForResult if you need a notification of available results.
The numeric parameter is arbitrary and indicates a request code. In other words, the same handler will be invoked for any results from any launched intent. The request code is a way for the handler to select notifications and ignore those that just don't apply. The notification comes through a protected virtual method of the activity which starts the intent - the onActivityResult method.
The resultCode parameter indicates whether the user accepted or rejected the photo. It is -1 if you use clicked on Done; it is 0 otherwise.
Displaying a Preview
Displaying a preview of the photo taken is really easy. At this point, the full image has been saved already to the specified location. You can retrieve that one from the SD card or grab a thumbnail from the intent. In both cases, all you do next is displaying that image through an ImageView widget. The following code shows how to retrieve a thumbnail from the intent.
In Android, a Bitmap is a plain matrix of pixels. The data you get from getExtras is not compressed or transformed into a common use format like PNG and JPG. Figure 2 shows a preview.
Figure 2: A just taken photo is previewed.

Sending the Photo via Email
The final step is connecting the camera intent with the email intent to attach the just taken picture to an email message. Also in this case, we'll be using the system native application to send emails. Here's the code you need to prepare and launch the intent.
In between, you configure the email adding attachments and recipients. Here's some code:
The setType method indicates the type of the attachment. The various calls to the putExtra method set specific pieces of information for the intent: email address, subject line and text. The attachment is appended as a stream. The reference to the file is obtained via a stream. Figure 3 shows the email screen. As a user, you hit Send and the email will go.
Figure 3: The just taken picture is ready to be sent over email.

Summary
Using intents is clearly not the same as using the camera API and gaining access to the camera stream. The intent offers a sort of black-box with limited flexibility. This black-box, however, is good enough for a bunch of applications as this example demonstrated. The camera API offers a lot more such as the ability to control the preview stream and the overall user interface through which the user accepts or denies the picture. You also have access to the picture stream before it is saved to the SD and can apply any transformation you like. But that's makes good fodder for another article. Stay tuned!
The Android for .NET Developers Series
Part 1 Starting with this article, I'll discuss what you need to know to approach Android programming without any aid from your .NET expertise.
Part 2 In this article, we'll go through an Android application that accepts input from the user and handles user's clicking.
Part 3 In this article, you will learn how to build the user interface.
Part 4 In this article, I'll be delving deep into menus and dialog boxes in Android for .NET, and discuss a few very common (and frequently used) types of menus and dialogs.
Part 5 In this article, Dino Esposito focuses on the options that the Android SDK offers for local storage.
Part 6 In this article, Dino Esposito shows how to build settings dialog boxes using a built-in feature of Android for .NET.
Part 7 In this article, I'll dissect the code of a realistic application - a waterpolo score manager - to show how to save and resume the status of a game using both the internal storage and SD card.
Part 8 In this article, I'll focus on the execution of common tasks from within an Android application. I'll build the skeleton of an application that takes a photo and emails to the specified address. For both tasks I'll use native Android components.
Part 9 In this article, I'll discuss how to arrange an Android view where the dominant element is the list. I'll start with a plain list of strings and then improve up to populating a listview with downloaded content arranged using a custom layout.
Part 10 In this article, I'll discuss the features and capabilities required by an Android application to talk with Twitter. In particular, I'll focus on authentication and updates.
About Dino Esposito
 |
Dino Esposito is one of the world's authorities on Web technology and software architecture. Dino published an array of books, most of which are considered state-of-the-art in their respective areas. His most recent books are “Microsoft ® .NET: Architecting Applications for the Enterprise” and “...
This author has published 53 articles on DotNetSlackers. View other articles or the complete profile here.
|
You might also be interested in the following related blog posts
Simple Task Scheduler inside your application (C#)
read more
|
|
Please login to rate or to leave a comment.