Best Android Studio Tutorial on how to Upload Image/File to Server (Part 1) - Camera & Gallery

Best Android Studio Tutorial on how to Upload Image/File to Server (Part 1) - Camera & Gallery - Hello friends Android tutorial and tips trik, The article you are reading this time with the title Best Android Studio Tutorial on how to Upload Image/File to Server (Part 1) - Camera & Gallery, we have prepared this article for you to read and download the information therein. hopefully fill posts article -, article (Part, article 1], article android, article Best, article Camera, article How, article Image/File, article on, article Server, article Studio, article TO, article Tutorial, article Upload, we can write this you understand. Well, happy reading.

Title : Best Android Studio Tutorial on how to Upload Image/File to Server (Part 1) - Camera & Gallery
link : Best Android Studio Tutorial on how to Upload Image/File to Server (Part 1) - Camera & Gallery

Baca juga


Best Android Studio Tutorial on how to Upload Image/File to Server (Part 1) - Camera & Gallery

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.

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.

Okay.

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!.



Thus Article Best Android Studio Tutorial on how to Upload Image/File to Server (Part 1) - Camera & Gallery

That article Best Android Studio Tutorial on how to Upload Image/File to Server (Part 1) - Camera & Gallery This time, hopefully we can give you all of the benefits. Well, I'll see you in another article post.

You now read the article Best Android Studio Tutorial on how to Upload Image/File to Server (Part 1) - Camera & Gallery the link address http://svedda79.blogspot.com/2016/02/best-android-studio-tutorial-on-how-to.html

0 Response to "Best Android Studio Tutorial on how to Upload Image/File to Server (Part 1) - Camera & Gallery"

Post a Comment