Skip to main content

Attaching a Photo from the Camera

Lesson 29 from: Building a Twitter App

Tony Hillerson

Attaching a Photo from the Camera

Lesson 29 from: Building a Twitter App

Tony Hillerson

buy this class

$00

$00
Sale Ends Soon!

starting under

$13/month*

Unlock this classplus 2200+ more >

Lesson Info

29. Attaching a Photo from the Camera

Next Lesson: Posting a Photo

Lesson Info

Attaching a Photo from the Camera

next step, everything is gonna be pretty similar. Except we're going to attach a photo from the camera instead of the media library. So we're gonna add a camera button to this pop up and choosing camera will open the camera application and allow the user to take a picture. And when the user is ready to use the picture through the normal android interface, they will be sent back to this screen. Muchas if they had chosen the picture from the library instead, you can see in the middle the middle here. This is what the camera application looks like on the the emulator. It may look different on your device, but once they take a picture, um, they'll get sent back here just like they chose it from the library. And we'll have a picture that is actually stored in the library for them to post up to Twitter. So just a quick note about this. The way I'm doing it is how I wanted Android to do this. This camera, um, like taking a picture and grabbing the results of the picture from the start, but is...

only available from, um I think two point two point over 2.1. I'm not quite sure about that. So you weren't able to do this with a full size picture before in the past. And I don't I don't even know if it's supported on all devices. So I just kind of showing you a, uh this is a nice, easy, good introduction to working with media on Android, But it may not always work in all cases. So that's something you want to test on devices? Um, what what it will do instead of working, um, on older versions of the of the operating system and on some devices that don't yet support the way this works, Um, you'll you'll call out to the camera application and say when the when you're done, send me back the photo and we'll give you a smaller version, not the full bits that the user actually took with the picture. Which is kind of annoyance. Um, I'm not quite sure why that happens. There's actually a really long ah bug report an android's bug based on Google code, but that I I have voted for and put comments on and stuff because I really would like that to be fixed Um, and there's not really any sort of official word on why things happen the way they do with using the camera. So if you're familiar with IPhone programming that you know that if you want to the user to be ableto to take a picture in your application, you can just say, Take a picture and it will come back with the bits of the of what the user took and that's that's exactly what we want to happen in Android. But it doesn't always work that way, so sometimes you'll get the the smaller version of the image coming back instead. Um, if you want to have full control over the camera, it's also possible, and we can. We can talk about this more in the forms if there's interest. Um, it's also possible to create your own application that you kind of you get access to the camera as a service and then sort of just update the bits of what the bites of what the cameras seeing on something called a surface. And then you can have controls that allow the user toe toe, work the camera and zoom in and and all that kind of stuff, and then you have access to the full bites of the image yourself, which is a lot of extra work to do something that I think should be something provided by the operating system. Um, so anyway, that's kind of a caveat. What we're doing here may not always work the exact way that you expect. You may get a smaller image or it may not even work on older versions of the operating system. So let's look at the code. I'm sure the maybe some questions about that, but we can go into those in a little bit. Let's look at how it actually works here in a second. Once we look at week five, step three, we're gonna look at the post activity. Um, Now you can see. And this is kind of an answer to that last question I now have to Constance for for, um, requests that will be sending out one is to choose ah, photo from the library and the other one is to choose a photo from the camera. And those need to be separate. Unique. Constance, Um, now on activity results, let's just go into that first. Since the question was already asked. This is the like, the end of the story here when we call back to call out to the camera application and the user takes the picture and everything is ready to go from there. Well, get on activity result called, but with the the different quest code depending on, um, how we kicked it off. So we went to the camera. The request code will be the request code. Choose photo from camera. If we went to the library, it will be from the library. Now, the reason that I have this differently is because, um the reason I have this set up differently is because the your I is not gonna come back from the camera and quite the same way. Um, let me show you how it actually works. So what I'm doing is storing that you're I earlier in the process because we're not going to get your eye to anything because the user just took a picture from the camera. What we need to do instead is to, um, wait for the media service to go and register that that new image was added to the library because we're gonna add that to the users Media Library. So, um, hopefully that answers the question a little bit. That's kind of at the end of the whole process here. Now let's go look at it that the process from the beginning and then make sure it makes sense to everybody. So where the whole thing starts is when we do that dialogue builder. Um, and let's see. Photo button clicked. Post photo button clicked on line 1 96 of post activity when that post photo I'm sorry when the photo button clicked. Ah, method is called check to see if we have credentials again. And then we build the alert dialogue to allow the user that shows choose the method to get the photo. Um, now we have three buttons instead of two. So the positive button, which in this case just means the left most button we want to be camera. The neutral button, which is the middle button, is gonna be photo library, and the right one will still be canceling. These will all work the same. Um, but the camera button is going to use this click listener defined in camera button click listener. So if I hit F three on that. You can see that online 20 to 41 That just basically calls out to this open camera method on t 43. So if I had have three on that, you can see that method is defined on 1 75 So open camera. Here's what I'm doing here. What I'm doing, um, is opening the, uh, the camera and passing in some information. Um, that I want the camera to use to store the data that the that the of the image of that the user takes through the camera. So I'm getting a few things together here. I'm going to store the They're going to make the name of the file. Um Oh, tweet, underscore. Photo underscore Time stamped the timestamp of the photo that was taken. So that way we can have a unique file name for each photo that's taken through the O tweet application and this content values you can see. Ah, that's kind of like a hash of things we're gonna put into this Ah, into the, uh, the Miss method Here. Insert. So, um, the so ah, these air piece of information that I want the camera to store on the media that it's gonna be taking. So the user snaps a photo. I want to make the title of this, uh um, new image that the users taking a tweet photo with the time stamp. And I want the description to be image posted from oh, tweet, which is a string that have defined here. It just says image posted from a tweet. And then, um, what I'm going to do then, is get a your eye that would represent where that image would be. Um, so this is before the image actually exists. But I want to get that your eye and what it could do to do that is call out to the content resolver which is accessible by a method on the activity. And I'm gonna call insert. I'm actually I'm actually creating this. I'm not just getting the your I'm creating this this year, I which is gonna be completely empty but that I'm gonna pass this on to the to the camera and say, Put the data that you get inside of this year I that I created through the media store. Um and that way I have a unique ah file name and stuff for the for the photo bites to go into. But I also have a your eye here because if you remember, when the user taps on a photo from the library, need to get that you or I to make the code work. So this is gonna work exactly the same way off that photo you or I once the photo from the cameras taken. So what I say is in this insert method, I'm going to create using this media store images, media, external content, your I, um constant, which points to some any, uh, external content of type images. Type media for an image is on the the SD card. And then when I put these values in here on the insert statement, which underneath the covers, you can probably guess that this is using something like sequel light to store all this information and reference all the different things on the user's library, like videos or images or or content data, things like contact data, things like that. Um, when I passed in these content values, if you if you remember back to the first class when we dealt with local data with sequel light, we use content values to deal with placing data into the different columns of ah, of a table. So this is working kind of the same way. I'm going to say that the title on with the title and description to be this. So go insert that and tell me what the your I would be when that's inserted. So now I have a reference that you or I and then I could go on and create an intent. And this is where we actually open the camera. Um, the new intense of type media store action, image capture. And then I'm gonna put an extra, um which is got the constant, um, media store extra outputs. I'm saying open. I want open the camera and I want you to put the output of whatever the user takes into this your eye, which is stored in photo your I So, um, once the once the camera takes a picture of this will be the your eye that I'll be able to reference the image that the camera took, and then I'll be start activity for result in passing the request. Choose photo from camera type as the request type, so that will actually open up the camera until the camera, all the information it needs to put the image in the right place. Um, the next time we'll see action here is an activity is back again on on on activity result, which I kind of already went through. But once again, in this case, the result code I'm sorry. The request code will be the request that week that kicked the whole thing off, which is request shoes, photo from camera. And in this case instead of ah, when we came back from the library, we had to go get to your eye, um, out of the intent that came back. In this case, we already know the your eye as long as everything went OK on the result code was okay, um, that we already know the your eye that we want to work with. So we use photo, the post shows chosen, um, and pass into your eye, which is the exact, um, method that the library calls so that we can get back into the to the get out of the divergent code flow. And now everything works exactly the same as if the user had worked with the library instead of the camera. And now we have a your I, which we can get a thumbed a lot of, just like with the library. And ah, again we get the ideas, the last past segment off that you or I make a thumbnail with this little thumbnail service here and then show that instead of the button background and now we're pretty much back at the same spot that we were as if the user had chosen something out of the out of the library. So let's run this code and we'll see instead that we're get it from the camera. So I'm gonna run step for await us posting. I need to actually run step five. So I'm saying stuff three. So Step three run as Android application. And now, once this gets ready, he has deployed. We'll see that instead of just having one option like we had before. Teoh, pick up Some media from the library will have another option to pick from Thea camera. So it's the tweets were loaded. I can press menu come here to post and I can click attach photo. I could choose the camera this time and Now we get the emulators camera, which again may look pretty different than switch that orientation here. This may look different than what you can see and your device, but this is what you'll probably see on the emulator. Just kind of looks like Hey, something's moving around. Look at this. Crashed. I think we can still kicking things off again. Okay, so this is just sort of kind of preview, some motion on the camera, and then if we press the shutter button, you could see that it takes the picture, and we can choose to cancel on, retake a cancel or re take a picture. Or we can choose that that this is exactly the picture I want to see. Um, you know, kinda looks weird here, but when we press OK, you can see that it actually puts in this. Ah, if you remember, um, this was maybe two years ago. Now Google put out ah little comic book type thing that kind of describe what the android platform was about. And this is kind of a screenshot from that from that comic book. But now we have the ah, the bites of that image as a thumbnail at least, and we have a u R I pointing to the actual bites. So we're ready to upload to Twitpic, um, either from the library or from the camera. So sort of, in summary, you can his action image capture with an intent to capture an image using the cap using camera. And you can give it a your I, uh, starting with a key of media store extra output to tell it where you want the camera store the image once the images taken and then you'll get a your eye out of that to be able to reference back that image. And once again, all the caveats applied that this doesn't This is exactly how I think the camera should work, but it doesn't always work that way. On earlier version of the operating system and even on some some cameras are some devices with the latest version of the operating system. Okay, so any questions about how that works? Well, I had one, but it seems that you just answered it. Okay. Um, well, we'd still go over it anyway. Just make sure it was what line and where is the command actually calling the camera. Okay? Yeah. Just K series. Um, missing that, Um, I gotta find it too. So that is post activity. Um, 1 75 is where open camera finally starts to do the work. And then on 1 83 is where we, um, construct the intent that will open up the camera and the setting A would like you to explain again about the get contact resolver a p I. Okay, so get content. Resolver is ah, the content. Resolver is is a helper class sort of to deal with the media a p I or not, not just media ap I any. Any content on the device which, um which would be like, you know, um images, video, MP threes, Um, maybe some documents or something like that. Um, and what it is is just a way to use either put in, like through inserting or delete content. So I can delete content programmatically through the content result. Her Aiken add content through the content resolver through inserting. I can also select second, also quarry some content. So give me a list of all the images. This would be what you would use. Go look at the A p eyes for this and on the Google docks, Um, for the Google docks, the documentation for Android And you can see that this would be what you would use to get a list of all the the images that you could read and write from that were of type, you know, paying or J pay or any type or something like that to basically build the library, the content library view that we already saw that that's part of Android. So that would be how you could build some sort of activity toe. Let the user pic images, but not go through the regular android, um, views so that they were they were scannable. The view is scannable, or you could do some extra special things there. Whatever, but but the content result Her is how you get access to all those different pieces of content. And we're using it here specifically to store some, um, or to create a piece of content that's reference civil through the content. Resolver um, we're storing it on the external device. So, um ah, and they were passing through this information that we want to store about the content. So then the content result was responsible for going to put that on the extra advice, even though there's nothing there yet, but But it'll at least give us back to your eye. So it's responsible for for all those things for finding and leading and dealing with content and also pointing at it with your eyes. Is that a P I provided by enjoyed itself? Yes. The content result is not something I created. That's this this method is on on. Ah, Well, I guess it's on context, Rapper, which is I think something that activity extends. Okay, um and I another question, Can you store the captured photo as a raw file in the file system instead of content library? Yeah, and I don't know if you'd be able to, um Well, actually, you think what you would do is use the content resolver to insert into the your local directory that the application has instead, I'm not quite sure. I'm not sure I wanna live code that but we can talk about it in the forms, but basically you can use the content result were to do the same thing, except instead of storing it in the external. Ah external storage would store it. Um, with that you are I that points to the local device or to the local applications home on the device instead. And then you could read the bites out of you could even go so far as to make it private. Um and ah, there's lots of different and can't ations to deal with their Let's let's take that to the forms. Because I'm sure other people may be interested in how they would create their own activity. Um, for using the camera and dealing with local media bread. I have three questions in the chat room. The 1st 1 is Ah, not in the open camera method that you're using. But the other method that you were using media object. What is the media object? Actually, in that method, I didn't have any specifications on exactly where it was. Um, that maybe faith, but what is defined in the media now? Come on for I hate this. Come on. Like what year is that? We have to pick which direction researching it won't won't won't just wrap around. If I wrap search, I guess I could do that. Here we go. That's one of them. I don't think that's what they're accessing or what they're talking about. Oh, maybe this one here possible? Well, basically these air, um, just classes that have a lot of static constants, a lot of constants that refer to Teoh things that you want to deal with in the content resolver and also some helper functions. So I think, um, I might have actually created the no. I guess I create the thumbnail through Media Store. But media stories, just like media, has the same a bunch of different constants and helper functions to deal with different types of media. So that's why there's this kind of hierarchy media store images. There's also see if I do some code completion, um, going past all of the constants. You can also see that there's ah media store, audio class and immediate start video class. So those are the different types of ah of content that the media store has some has some helper functions and constant constance to deal with. So is that a static object to the post activity, or is that actually a property or a field of the post activity? Ah, that's a in her class public in her class on the media stories, the media stories, a class that has a bunch of Constance and and different things. And it turns out that it just has three inter classes, images, video and audio, which in turn have other interesting things kind of in a hierarchy of the way to. It's basically a bunch of global global constants of global functions that you can use to deal with content of those different types you'll find. You'll find similar things, I think, for contacts don't if it's contact store contacts, contacts. Yeah, contacts android dot provided a contacts. Contacts like your your people in your address book. Um, you want to go find Thea the email for one of your contacts or find funny every contact that has an email or something like that You're working with those kind of the exact same way that you do with media store and context is is a constant. I mean, it's an object class. Sorry, a class that has a bunch of those helper constants helper methods. Um, like, say, here kind phone. I want to find contacts with the I Look at this, the content so contacts is deprecating They want me to use contacts. Contract instead. But in any case, it will be the same kind of things over there. Um, I gotta find contacts that I want to deal with. Phone numbers, Something. I may use this this kind phone to go search for different information about contacts. So that's kind of a way that android has these global helpers to deal with the different types of information on the user's device. Okay, Thank you on that one. Ah, The next question is, um, don't you have to put Thea in the man application manifest that you're going to be using the camera? That's a good point. Let's check to make sure I didn't just completely space talking about that. So Okay, I do not, um, for the way that I did it, which is to call out to the camera as a service, kind of as it were, and say, Ah, when you're done, give me the picture that the user took. Now, if I were to create my own camera activity, um, and then and then get access to what the camera was seeing and being able to control the camera like, say, take a picture or zoom in on this point or something like that, Then I would need to put in a permission, uses permission. But my application is not actually using the camera in this case, Android, the android camera application. I'm just calling out to it and say, Do your thing, and then give me a, uh um, actually, Well, yeah, actually, do your thing, and then put the bites that you get into this your eye. So in this case, I don't need to add another permission. Would it be a good practice to call that out in your permissions list? Anyway, um I mean, what the point of those permissions are, um t let the user know that you'll be using. If I had access to the camera in my application, I could still sort of surreptitiously used the camera even and not give the user any indication that I was doing that. So that's why I would have to call out and say that I'm gonna be using the camera. But in this case, I can't use the camera without going to the android camera application for it to take its picture. So I'm not really doing anything under the table. I can't. I can't sneak anything past the the user. In that case, they know that they're They're all of a sudden in the camera application, and they can choose whether or not to take a picture. So I'm not really doing anything that I that that, um, Google, in order to protect the user wants me to do to make sure that they know there that I'm gonna be doing something special. Um, does that kind of makes sense? I'm not able to sneak anything past the user, so I don't really think it It matters. I don't think you could, but then, um ah, I don't know, just something extra to do. It wouldn't really hurt one way or the other. Okay. And ah, last question about the about the application manifest flag debug able equals true. Can you clarify that a little bit. Good point. Um, so deductible equals true. Just really quickly. We can take any more questions about this to the forum. Where is that? On application? I thought Ah, I guess not. Oh, I didn't set it. I must not have said it. I think it is on application, though. If I say android debug herbal equals true. That means I can debug using the I can attached to a d bugger on the device. Not through the emulator, but through the device. Actually, when I'm actually running on the device, this is pretty useful, and I encourage everybody to do it. Um, I mean to test on the device, at least and then sometimes nice to be able to debug as well, because things might be working slightly differently than the emulator. You never know. Um, but it's important to have this option and not always have it deductible because you don't want to ship a device the market and make a deductible. Otherwise, people could debug your application, um, off the market and maybe gain some interesting information to see how things are working or, I don't know, maybe even crack in past process, um, security. And do something. I'm not sure, but I've already like you, actually. Sagan Sorry about interrupting their but it's solely meant for device debugging then. Exactly what for? That's right. So you can still debug on the emulator without setting that flag to true. But when you export your a p caid for to put it up into the marketplace. It will tell you they will warn you. The compiler will warn you if you have that debatable flag on and say, Are you sure you wanna make this production ready? A p k D bubble, because you probably don't. Also, uh, something to note is that you have to actually put your device into debug mode to be able to actually debug the application as well. Yeah, that's in developer settings. You have to on the device, go to Ah, I think applications developer settings or something like that, and then allow debugging. So, yeah, that's a good point to thank you.

RELATED ARTICLES

RELATED ARTICLES