A free video tutorial from Atil Samancioglu
4.7 instructor rating • 22 courses • 246,159 students
Learn more from the full courseKotlin for Android O Development: From Beginner to Advanced
Learn coding and designing Android apps with Kotlin while building real app examples like Instagram and Foursquare clone
15:36:40 of on-demand video • Updated October 2019
- By the end of our course, you will be able to create professional applications and submit them to the play store from scratch!
- You will also have the skills of a professional developer who can earn thousands of dollars creating applications
English [Auto] Now it's time to select image from our users photo library. So it is a little bit tricky because we will deal with permissions and let me start by using the on click methods of selecting image. OK. So this will be select. And let me give the own quick method for saved by them as well. So this is say and go to my main two activities here and just create the functions of select that is you go by you k and it's and other thing is save again called by Rieu called you. So now I have my functions. But before I go on I write my select function one thing to remember is to add a new line and this Neve line will represent my permission my usage of confidential data. So we're going to see something called manifest here so we never work with manifest before. And here we have some information about our application general information and if we why use something that we can we have to ask for permission. Here we have to specify. So if you just type use permission you'll see you have a lot of options here. And what we want to ask for user is actually in this case read external search. So we will use the external storage for a mean of our user. Right. So you have to specify this in Android manifests. So after specifying disinformation you go here and check to see if we have any permission. OK. So you say if check self permission so did permission then I'm going to check it is actually this just type men first dot permission dot read X-URL search. Right. So I'm going to see if we have this permission from our user or not. So is not ok. Package Manager got permission granted. So this if condition means that if I don't have the permission to read externals church OK. So if I don't have it what do I have to do. I have to ask for permission Kate. So I will say request permission and it will ask me to give an input of aere Kaye and this will be an array of permissions actually in this case. I have only one perm shim. But anyway it has to be inside the array. So inside desiré I will say manifest per Hermitian that read external storage again and he will ask me and request quote This is just to identify this permission. OK. So we will use this and this is nothing relevant to the user. So you can save one you can say to you can say one thousand. I will say to Craig ands if I don't have it I will ask it. But Alice asked me as if I already have the permission right. So I will check to see if I don't have permission if I don't have it. I will ask it but as if I have it what will I do. I will just create an intent again but this time his intent is a little bit different. It will be intent not action pic. OK and what will we be picking. Is that me. Yes sure. OK. So didn't add. I just it's it's all to enter medius sure dot image is dot media dot external content. You're right. OK. So I will take user to choose a media for a media store. And this is actually an intent but rather than taking user to another activity takes user to media. Sure. This is nothing complicated. And this time I will say start activity but for results because I'm expecting a result out of his intent so intent is intent. And now I have to specify another quote OK. So I will just say this is one. And the usage of these request codes now would make sense. We have to use just two requests here. So we will check to see if we are talking about to request one or request to k so that disease y system keeps asking us from request coach. So we see we asked permission or we already have one. So only request permission results if you override dysfunction. OK we will see what will happen if we ask for permission and we will check to see if request gold is too. It means that we ask for permission. Kate and that is only the condition that I'm going to continue doing this operation and if I ask for permission and if ground results DOPs size is greater than zero and Grant results are the results that I got from back from user K.. And if they're greater than zero and if the first result I actually have only one result is package manager Dopp per machine granted. So it means that I have a result and the user granted me with the permission so I'm going to do this in turn think again in this coding book. Right. So you have already permission. I'm going to to the media store and if I ask the permission. If users use me then I'm going to do the same thing again. OK. And now I either have permission or or I asked and got it from the user that it means that I went to medius to her. Now I have to override on activity results Kate so that I will get to Chac If request code is equal to one the sign. It means that I went to the media store and result quote is does activity result. OK. OK. Think I couldn't write it. Activity dot results. OK. OK. So we have resolved Chen served. We have another seeings user can cancel his operation. We want activity results OK. And we have to check to see if that data is not novel. In this case we have something out of his media store. OK. And if these conditions hold. I know that's in media store. I have chosen something. So I will create an image from the data. OK. Dock's data and to convert this into a data. So now I how an image and this is the image that my user has chosen from that media store from his or her photo library. Right. So I will do. Now I will just open up try and catch Lukos it might fail this time as well. OK remember we used to try and catch one. If something's something goes wrong something has a potential to go wrong. OK. So I will create a selected image with media store dot images dot media dot get map. So the bitmap that I'm getting will use the content resolver and it will drive the bitmap from the image itself. I will say image view outset even map selected image and now I think we are already right as I said before this may fail so I will just write everything inside try and catch here and it will be an exception. OK. So you can say preens set trace and copy this cop this story. I'm pacing inside try so that we will not have a crash but something else something goes wrong in this operation. OK so now let's try and see if our user will be able to choose another photo. A photo really from their photo library. So say at art and click on select image. And as you can see it asks for permission. OK. And if you are a low user if you allow your application we can now see the photo liberate here. So two clusters of yes sure. And here I have two pictures and if I select one as you can see now it is in my image. You can say this is very nice that I can save it of course after you write the necessary code to save.