Skip to main content

Posting a Photo

Lesson 30 from: Building a Twitter App

Tony Hillerson

Posting a Photo

Lesson 30 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

30. Posting a Photo

Lesson Info

Posting a Photo

This is a pretty simple part made easy by the twit for J or twit pic for J library. Um, So what we need to do is, um, realized the fact that we have a photo to post and if we have a photo to post instead of posting through the Twitter for J library, we're gonna post through the twit pic for J Library. So that's gonna be pretty simple. We just need to figure out if we've got a photo, your eye and that means we've got a photo to post, and then we will send the tweet on through the twit pic for J library instead of the tweet for J library. So here's the link again to twitpic for J. Make sure you check it out. And I guess some people already have more than I have, obviously, because they realize that you can use a lot. And I had got that far either that it wasn't available and they just haven't announced it yet, which is what I was looking for. I was watching for, um, the documentation for twit pic Jay to see if it talked about how off. Um, but it may already have been an option. I just di...

dn't catch it. So, um, let's take that to the forms to because that'll be interesting. And then we can work on getting the application ready for O off for posting photos as well. So the way that I post photos is gonna work exactly like posting tweets were going to use an a sink task. Um, except we're gonna use a different a sink task that there was a post photos, and it will have a similar sort of inner inner interface defined post photo a sink task post photo responder that the ah, the the post activity will need to ah, implement. And then it will take some object for its input. It will not provide any. Um ah, specific progress. It'll just, um So we're for that middle call back. If you remember how it works, we're not gonna have any when I could have any progress where I could report any progress. And then once the response is done, we're going to send we're gonna give back a twit pic response. And, uh, that's how the a sink task will work. So let's go Look at that. A sink task code really quickly. And then we will look at the post activity where it's implemented, and then we will post a photo. Okay, first step here is in comma Riley Android. Oh, tweet tasks. There is a post photo a sink task. Um, the way that works is like I was saying we taken object. Um, we will not return any you think progress? Because when we're doing a network operation, we don't know how long it's gonna take. And then twit pic responses What we're gonna return. The constructor takes a post photo responder, which is defined down here online 30 which we've already seen visited. Callback methods will call once the Poteau. Once the photo starts posting will call back. And once the photo is completed, posting will call back with a twitpic response result. Um, and then the constructor also takes a context, and it also takes a twit pic object. So this a sink task is not responsible for constructing that that twitpic guy to go on post things too. Um, somebody else is responsible for that. Um, but then remember, basic tasks have this method doing background, which is where the actual meat of the operation is what were you remember that takes of our our eggs. Variable argument. Um, it will return a We will take an array of objects, which allows me to I've dropped back to the type of objects, like a pass in different types. It's because I'm gonna pass in a string and a your I The string will be, um, the contents of the tweet the your I will be the location of the photo on the device. Um, that else. Then I go through and do some grunt work here, declare a an input stream and a bite array, and then get a content, resolve her off the context because we need to get access using your eye to the content we're gonna post, and then we'll construct a twitpic response as well. And then inside of a try catch where that could be a couple different things that go wrong. Um, catching the exceptions, we're not really doing anything special here later on, that's just to cut down on the amount of code, but for production ready application, we want to be a little bit We're friendly about any one of these errors. Um, at least you know. So something the user here, I'm just gonna print a stack trace for debugging purposes. But basically, what I could do with that you or I is if you remember when we When we, um when we use the library to get access to an image on the device, we said only give us ones that we can open and that allows us to call what we do online 45 through the content Resolver we can pass it in your eye and say, Give me an input stream with this with the contents of these your I and then just created bite array output stream, um, and read in the contents of the input stream into the output stream because we need an output stream, um, to pass the twit pic for J. This is just, you know, but you're boring little code where we basically get all the contents of the image into something called an output stream and then changed that into a bite array and then close up input stream and then ready to post because we need to give ah bite array to twitpic for J. So the teepee response is what we're gonna get back from from Twitpic once everything happens. Ah, an uploaded post. Basically you passing a bite array, which will be the the bites that we read out of that that input stream, and then we'll post the the contents of the tweet here, Um, as the second argument uploaded post. Okay, And then once we've got that to pick response back, we returned that a czar as the return value from doing background, which is part of the life cycle of a sink task will be then fed onto on post execute, which is called when everything's done and we're ready to post back to the to the activity or whoever called us safely. And, uh, what we'll do in that case is just call onto the responder and say that which is, remember, just this this type of something that implements this interface and was passed in and is a reference to the constructor something that we could deal with in a nice, um, decoupled way. Well, just tell their Sponder that were done. Um, so online 70 is kind of excess. The success case and online 76 is the the the callback that will execute before everything starts off so we can start showing progress or something on the post screen. Okay, now let's go look at the post screen and we'll see how that all works in practice. So that's in comradely and radio tweet activities. Post activity. One change here online 41. You can see that we used to just implement post tweet responder, which is from the post tweet a sink task. But now we also implement Post photo responder. And when we I believe it is called Post Button clicked when push button clicked is called, we say post valid tweet or worn, which is defined on 1 46 And that's where we decide whether or not the contents of the tweet are valid. We're gonna We're gonna give up if it's more than 1 40 or if it's zero. Well, say Hey, you need to post something. Um, but there's an interesting case. Now let's see, um, which we actually don't handle, which would be if you just want to post a photo to Twitter with no message. We're not allowing that right now, but you could definitely ease back the restrictions we have here to just allow you to post a photo. So now we would check something. Like if the photo you're I was not know, but the post length of the tweet was zero. Then we would go ahead and allow it instead of saying, Hey, you can't posted a blank tweet. But ah, we're still not allowing that. That's that's an exercise for the user or for for the viewer. If you want to get into making that a little bit more are a little bit less restrictive. But any in case the wife would pass all those those restrictions and the tweet will fit 140 characters and ah, there's nothing. There's actually something in the tweet. Then we come here and check to see whether or not we have a photo. You're I And if we do have a photo your eye, then we're gonna use this post tweet a sink task. I'm sorry the other way around. If we do, if we do have a photo, you are. Then 1 73 will be called, Um, so then we'll construct twit pic. Um, now, I don't think I think this is just this is just good code to be able to demo this. I don't think the post activity should be responsible for constructing a twit pic. Um, I think that should be passed off the application or something like that. But I just kind of consolidated. He here Teoh to show how the code should work. Um, in practice, that could be abstracted more to get some better. Um, but architecture in place. But in any case, I created twit pic here with the twitpic user. Name is twitpic Password past that into the constructor along with the reference to ourselves as the responder because we ah, implement post foot a responder here online 41. And then we kick everything off again back on line 1 74 by calling execute where we pass in the the tweet Teoh tweet and the your eye to the photo to post. And all of that takes place just like this. Said anything to the emulator. It's still thinks I have the latest code there. I'm not sure why. Maybe I actually dio So when I come here to click Post, I'll come here to the post screen. I can attach a photo. I was scared one for the library instead, and put in a they ah tweet message and you can see here. I also added, at Craig's request a nice little link that you can press to at our hashtag, um, automatically, which is nice. And this should be in the version that's put upon the marketplace. And now we have a photo to post and our hash tag so we should be able to click post. I hope nothing happened, so it didn't actually deploy my step for code. I wonder why not let me go do it the hard way. Here I'll come the week five step for photos project say run as Android application. Hopefully this will actually kick it off. Doesn't like that. I don't know why. Sometimes the emulator really gets flaky as we saw last week. Um, what I'm gonna do justice Just we can prove this thing works has come here to the the main screen press menu. Go to settings. Actually, no, I don't want to do this because then we have to put in all my passwords and stuff. Take it off screens. You guys can't see it and all that. Let's ah, let's just assume it works for now because because as we can see here. If I go to the home screen and run a tweet again as it loads tweets, you can see that I tweeted earlier Hero tweet Android posting pictures today. This was right before the class started. I wanted to make sure everything worked. Look at the detail here and look at this URL once that loads off a twit pic Ah, you can see something that I posted using the emulator before the class started. Ah, sticking a long time to load. There we go. So if I zoom in, you can see this would be the output of actually pushing that photo through twitpic for J. So it has the tweet here and here is the contents of the photo. Okay, so let's just assume that works, because I don't really want to deal with with this flaky emulator right now on let's go on to ask a few questions, Actually, first in summary, the content resolver allies, you get an input stream of your eye, provided that we have the proper permissions and you can filter once again, like we did with the library code to make sure that we have the the ability to open an input stream. OK, any questions about what we've seen so far? And we could talk about general questions and then wrap things up. Um, I will have one question from Victor. Um, why is Tony using a buffer of exactly four K doesn't have any special meaning or constraint? No, that's just a good good, because it's gonna, um, that's gonna read in that much. Just just something I probably pulled out of some example code or something, or I might have bumped it up from two. Do que, um, that's just how much it's gonna read in from the input stream during each pass. Um, and I guess that's somebody thought that was a good balance between being quick and taking up memory so you could tune that. That's tunable, I guess. Got anything else from the chat room? Bright. Apparently, I just still wanted its questions. That was the only question I had a kind of circulating eso that's just like the basically the upload buffer. No, that's not the upload buffer. That's so we have to get the bites of the Where am I? Here I am, so we have to get the bites out of the image and into a bite array. I can't just grab a bite array of this of this image, although there may be other ways to go about this by creating some other type of object. But this is just the low level, code toe taken input stream. And then, while there's still something to read, I'll read something into the spider ray. Um, this temporary bita ray um 496 bites their bites at a time so it reads, reads that chunk and then see if there's anything more if there is reached and reason other chunk of that size. And so you would tune this size of this temporary bite array to be what made sense for how much memory you had. So if there is a lot of memory, you knew you had a lot of memory. You could just make it as big as the image or making like you know, to Meg's Air two gigs or whatever, whatever, some something in between there that made sense, um, to make it fast because it's so this will be slowed down by the size of the bite array the temporary, By the way, this is I probably just copy this out of some code somewhere. So I didn't really make a big decision here, but that's probably a good, um good, uh, middle line between doing it quickly and and taking up a lot of memory because, remember, you can also run out. Remember, if you run out of memory and this than the A sink task is gonna crash, and android clamps down a lot on the amount of memory that you can go and grab. So that's something you've got a kind of balance for. I just gotta say, another question popping. Ah, is it Is it secure to add the O off dot properties in a production application? Um, I don't think that'll actually Well, yeah, I guess that will be running a red in at runtime. Um, that will be stored inside of the applications Private directory as well. So I mean, if you're on a non rated device, someone may be able to grab it. If someone gets access to it, it's not gonna be encrypted. So I guess, you know, not quite secure as as a totally bulletproof situation could make it, but but fairly secure, because no other applications gonna be able to read the contents of that directory

RELATED ARTICLES

RELATED ARTICLES