hello everyone! my name is oum saokosal. today i am gonna talk about how to uploadimages to server. but for the first part, i am gonna focus onhow to get images from camera and gallery. for a camera, you need some permissions.
Android file transfer, you need to know how to dispatch a camerausing intent, how to get images from the onactivityresult(), and you need to know how to load image andyou can put it on the image view or you can just upload it to the server. for a gallery, you don’t need any specialpermissions.
but first i need to show you how to uploadsome images onto an emulator and you need to know how to get the gallery open and it’salso using intent and get an image on and may with the first call activityresult() againand load the image and do something with it. actually you need some code and some stepsto do that but i decided to make my own library and this is my own library and you will useit for this tutorial. okay, i just created a new blank project andi just updated. so, let’s check it out. so, now the library is 23.2.1 and before igot a problem with the user interface here with the layout is it doesn’t work well.
and if can change back to 23.1.0 like thisand it works well for me. so, if you’ve got a problem maybe you mayneed to change back but for now my id is updated so i can use the user version. so, it is good to go. so, first you need the only three button andlet’s use the image view maybe just change here in the code better. the app android here means that you use theimage from the android library and then you need the camera and you can see over hereit’s a small image or maybe you can just use the preview, okay and you need the maybea gallery.
okay this one is camera and this one is galleryand this one is for upload and therefore here we change the source here, gallery and thisone is upload and here you use the youtube upload here and just the image. nothing special and lets run it first. i’m using genymotion and if you are newto this maybe you should stop the video and go to the website genymotion and downloadit. this is for free. use and get a faster emulator. you should not use the nettie one.
again this is the camera, gallery and upload. i think. oh i change the name and if you see somethinglike this, you should just press the refresh. so, i call ivcamera, yes, ivgallery and ivupload. you need to get the path that is imageviewivcamera, ivgallery, ivupload; and this is for menu, i don’t need it. ivcamera = (imageview) findviewbyid (r.id.ivcamera)and press and load. okay, so first you need to enter for camera. ivcamera.setonclicklistener() and then usethe anonymous class here and one click here,
okay. so for a camera you need a special permissionand if you don’t know you can go to my github and check out some documentation here youcan go to reports here and you can find out photoutil which is a library for tutorialand so what you have to do is to add three permissions and this one in the android manifest. okay, first we add it for camera and secondone is the feature of camera, and this one is the permission and third one is to forwriting to external storage. okay. so, let’s go to android manifest here.
so, uses-feature android: name and type androidand hardware and camera. if you use the camera like this, it’s notgonna work because this one is dedicated to this one and so you need to use camera2 andit is required too. when you try to upload this project to yourplay and if the hardware doesn’t have any camera the user cannot download it but fortunatelyeveryone of android device now has a camera and in case you don’t use that camera much,you need to set it to false, okay or is up to you but for now it is just close and thisis the purpose of the tutorial that we mostly use camera or uses-permission and just typecamera there and then press and then you need to do another permission to write to the externalstorage, okay and its done.
the next thing you need to do is to dispatchcamera, so i let to use this library and i need to explain you a little bit of it. here when you use the intent, okay, the intentis like this but there are two intents: first is called the external intent and the otherone is the internal intent. we are gonna use the internal intent. it means that we are gonna use some hardwareinternally and for that we need to call to the camera and let’s say i forgot the code,let’s say, camera, okay. for example, usually we need to call startactivity and if you want to move a class, let’s say you want to open another activityand this is the main one, okay and maybe you
go back to the subactivity class. and you just call the startactivity becauseyou want to move to subactivity. okay but at this time, you need to open acamera and then when you close the camera you want to get the data back, so that’swhy you don’t use the startactivity but you use that activity for result but insteadyou can send the maybe you can open a new activity but then you want to get the resultback. for that you need to write some code and ifyou want to explore some code then you may go here and check out the code behind thatbut i don’t want to waste the time and just downloaded and let’s show you how to usethat.
but while i downloaded i want to point outanother thing that now you call startactivityforresult but where you get a result? for the result, here is outside here on heremain, you go to code, you go to override methods, and search for you need a method called onactivityresultand so it should be around here and let it would be okay and here you go. so this is the result that you get back fromthe camera, okay. so you can do anything with that and to opena camera you need some code and that’s what i said and that’s why you need my library. so, i downloaded already and unzip it.
and so you need this one photoutil and copyand you go to project and you go to app and go to libs and paste here. right click on here and at library and then you can go back to android and check the gradle on here or you add the photo thatyou’ve already. so, here what you have to do is you can callthe photo camera photo library as well and so you declare maybe camera yes cameraphotoand upto this and run here cameraphoto=new cameraphoto() and you need to put the contextand the context here is this and if you are using intent in a fragment and maybe you canuse the applicationcontext, okay, it is safe to use the applicationcontext like this andnow you can get this one camera and then i
call takephotointent, okay. oh this one need an ioexception and let me check the code little bit and put it then yeah you need the camera request andthis is the response code for that. so it’s kinda like a integer but it mustbe the same from here and even here on create results, so let’s declare it after finalinteger and just call it camera_request and you can just give a number for everyone andyeah like that, okay, and this one should be toast and okay so you can get the resultfrom the camera and but let us run it first so what’s it gonna be like when we startand just know like this, okay, and so click on the camera and you can now see the cameraalready ready.
so, we click that and click on this and youwill save it and so here the takephotointent(), it’s dispatch a camera as you can see hereand then gonna save the image onto the external drive. okay, so here you can get the results. so first you have to test the result codeso call to result_ok, it means that you get the result okay and then you need anothercode and it’s called requestcode and this is the request code that is the camera requesthere. okay, so the camera photo, the library andnow you can get a profile and this one you can at a picture that you has taken to thegallery, so the next time you can browse the
pictures from your gallery, okay and that’sit. and this is the photo path and you can youknow you can load the image and then passing into maybe activity or maybe uploader. so, let’s create an image view again hereto show the image. okay now i added a new image like this andit is a star big one and then i have it here and then i right click and then call ivimage,okay, so we need to get from here and well okay so we get from the viewid as well andover here now you get the past maybe better to do the log and i need to have a tag hereprivate final string tag = this and so that i can get a name here and then put a tag here,so maybe string photopath.
okay, the problem here is actually i justconfused with the resultcode and requestcode. okay, the second one is requestcode, so clickagain and let’s see this is the image and them now you can log the image here. so inside my library, photo youtube you canhave the camera, gallery and also the image loader to load the image. to do that is very easy and you need a bitmapand then bitmap like this, then image loader and first you need from what file and thisis the file name and you need to scale it because we don’t want to load the wholeimage and then maybe its size is you know 2 megabytes and you compress it into the screen,it take only like 200 kilobyte or 20 kilobyte
of that, so you need to scale that or youneed to request the size so what is your screen size okay, what is the width and what is theheight and this is the request size and not the actual file. i follow the material from the google andyou can find the android camera loading efficiently and you will see the material about that. so i was inspired by this okay, so i willgive the size like 512 and then you wanna get the bitmap. so you need to try by the exception righthere as the file might not found right. so, typing something and look at here, nowyou can load the image into the ivimage and
call setimagebitmap() and then okay. take the picture. see so this is the image that you request,the size you request was 512 x 512, so you need some more maybe you need to increasethat but make sure you have to put the sign here otherwise it may gonna have some problem. that’s all with the camera and now we aregonna move on to the gallery. for the gallery, gallery is quite similar. over here, now this is for camera, ivgallerywetonclicklistener(new), control space and here you need to call the library galleryphoto,so let’s create a gallery request here and
the name doesn’t matter, you canjust giveany name and the number also just any number. here, you can galleryphoto new galleryphotogetapplicationcontext here and on click you can call startactivityforresult gallery photoopen gallery intent, you need to surround by the try catch and you need to have therequest code galleryrequest, yes and then here another if lets create it and you cangalleryrequest here and inside here request is similar you can photopath and then galleryphoto get path then the code should be the same. yes, i think it is the same, maybe a littlebit changed. so, let’s click on it and here somethingis missing and i think what is the problem?
maybe the code that i do, maybe yes, thisis the image that was taken, the result that you add this okay, add to gallery and if youhaven’t called it then the image is not gonna display here but it just gonna a simpleone but you need some real image but let’s say you choose an image first and okay someother error, what is it? 95 here. okay because i don’t call setimage uri andthis is the uri that i get from here and then data. getdata and then here setphotouri and choosethis one, okay, lets upload image and so you go to this button, see android ui monitor,you need to start the emulator here you don’t
start the image gonna, so you need to uploadthat image and so another thing is that, for example, you have four emulators here andnow i run the nexus 5, when you upload the image to the nexus 5, you can see the imagein the nexus 5 only and not the other. make sure, you use the one that you alreadyuploaded the image to. so, you go here and you can see the sd card. so, you click on this button, push the fileonto the device and you can choose any image, so let’s try one or two. one by one i think and its angkor wat andits gone well. we have to restart it and so we have to restartfrom the genymotion library here and then
run it and then click on the gallery, maybebecause i didn’t choose it so click on the gallery, okay now you have the gallery imagehere and so let’s choose the angkor wat, the temple in cambodia, my country and nowyou can see the angkor wat. okay that’s it for today.
if you like my video, please don’t forgetto subscribe my channel at youtube user oum saokosal. okay and we will see you all in the next videoon how to upload this image onto a server. thank you!