From the course: Android Development: Understanding Intents

Starting the camera - Android Tutorial

From the course: Android Development: Understanding Intents

Start my 1-month free trial

Starting the camera

- [Narrator] In this chapter, we're going to start learning about how we could use some of these standard system intents to perform some common actions and we're going to be focusing on media-related features. Remember in the MainActivity and here in my start version, I'll open the MainActivity and let's just make that visible. Remember that here in the MainActivity there are a couple of button handlers, here's one and here's the other, that open some other activities in our sample app. So for this chapter, we're going to be working in the MediaActivity class so let's go ahead and open that. I'll open the activity media layout and I'll open the media activity code. Okay, so let's scroll to the top. Here in the layout for my MediaActivity, I have some button controls to test out some intent actions as well as a placeholder image view control that we will see how to use later. In the code for the activity, you can see that we've got some code that sets up the UI handlers for the button clicks. Here's the image view that we'll be using later. And we've got the code to handle the clicks of the button in the activity and we're going to start off by seeing how we can ask the Android system to start up the camera so the user can take a picture. Now, note, this doesn't actually cause the picture to be taken or the taken picture to be returned to our app. We'll see how to do that later in the chapter. To start the camera in photo mode, I'm going to create an intent just like I normally would and we've already seen how to do this. I'm going to create a new intent and I'm going to use the MediaStore class to start up the intent action and this is going to be still image camera. This is the one that you want right here. Let me just close the project down right there, alright. For this particular action, this is all I really need to do so to kick off the intent after making sure that there's a app available to process it, I just call start activity like I normally would. What I'm basically doing this example is creating an implicit intent that asks the Android system to figure out which camera applications are available to take a picture and then start the intent which will cause the app chooser to be shown or if there's only one camera app then that app will be started. Let's go ahead and run this. I'm going to go ahead and bring up my app in the debugger and so, going to choose my emulator and we'll just give that a moment for that to come up. And after a few moments, our app appears in the emulator so I'm going to click on Media Intents and that will take us to our Media Intents activity and then I'll just click on start camera for photo. When that happens, you can see that the camera app starts up and I'll just allow the permission. Alright. You can see that since we're in the emulator, we are basically emulating a camera here so I can use my keyboard to look around this virtual room and I'm doing this on my Mac by holding down the Option key and moving the mouse around. Let's see, let's take a picture, oh here's a little doggy. Let's take a picture of the dog so I'll go ahead and click the camera button and the picture was taken and you can see that that worked. Now, I don't automatically go back to my application. If I want to do that I have to actually click the back button. Now, you might be wondering if it's possible to ask the camera app to actually take a picture and return it back to the app and that is possible and we'll see how to do that later in the chapter but for now, that's just a quick introduction of how to start the camera from within your app.

Contents