Best Android Studio Tutorial on How to Upload Image/File to Server (Part 2) - Base64 and PHP

Best Android Studio Tutorial on How to Upload Image/File to Server (Part 2) - Base64 and PHP - 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 2) - Base64 and PHP, we have prepared this article for you to read and download the information therein. hopefully fill posts article -, article (Part, article 2), article and, article android, article Base64, article Best, article How, article Image/File, article on, article PHP, 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 2) - Base64 and PHP
link : Best Android Studio Tutorial on How to Upload Image/File to Server (Part 2) - Base64 and PHP

Baca juga


Best Android Studio Tutorial on How to Upload Image/File to Server (Part 2) - Base64 and PHP

Hello everyone.

My name is Oum Saokosal so today I continueour lesson about how to upload image to server.

In this in this lesson I will talk about toencode image to Base64 string in case you don't know you cannot upload a physical filefrom Android to a web server.

Instead you have to encode that file intoa base64 and upload that string.

You can write your own library but I createda library for you here and then you need to know how to connect and android with PHP inMySQL.

It's not strictly to PHP in MySQL.

You can use your own language, in that I usea library Action task.

You can just use Volley, if you want to domultitasking.

But because I created a library for myself,so I just stick with that.

Lastly, I will teach you how to write somecode in PHP to upload that file and encode that file into the physical file.

Okay.

If you have any question then you can contactme on YouTube, Facebook or Twitter because this is the part two that I continued frompart one.

Here go to YouTube and find this one and youcan go to my YouTube channel and you can download the source code over here.

Well! In this lesson you need two libraries.

One is PhotoUtil from my Github kosalgeek.

Just download the zip file and the secondone is generic_asynctask_v2.

You can just use Volley if you want to insteadof my library here.

I think I downloaded already so this is thefile.

So just copy that and this is the first codefrom the previous lesson so I got PhotoUtil already so I just passed this new one.

Then right click at as library.

You can go back to android and double check-inand that is GenAsync1.

2 here.

So this is the previous code, you just runit.

So the last example, we know how to get the image from the camera and click it andthen we get the image from the gallery as well.

So now it’s time to upload it, first youneed to encode it so in the layout here, I have three buttons.

This one is camera gallery and upload, sowe are going to make a new one for the upload here.

So, upload setOnClickListener (new).

Right here you want to get the image here.

So you don’t want to get the bitmap becausemaybe you want to get a better quality of the image so this is chart for the showing,maybe you don’t need a bigger one.

So in this case I think I have to be clear,a string I just called selected and right here when you get photoPath and over hereas well.

So now you get the file name, so you can dosomething with that, so I want to convert to bitmap again, use my library image loaderinit() from selected photo and request right here maybe you want to scale up a little bit,so you are ready to upload that and it needs the try and catch here.

So right now I want to encode that Bitmapto a string, so I call it encoded image, so this time I will use another library ImageBase64which is which is the path of PhotoUtil and just use the encode(bitmap).

Okay, so click it, now you can see, this isthe encoded string of the image so instead of sending a physical file now and you cansend this and then into the PHP and you can use a (name) to encode the string into thereal or physical image, so let’s try the gallery.

Now you know how to do the encoding, thisone is part of my library.

So, now you need a PHP file before you canconnect to that, so let’s go to htdocs, make sure you are running your Apache webserver and port is 80, if you use another port make sure you use the correct URL.

htdocs,right click, new folder, so let’s call it just new and right click, create another newfolder (Upload) So for windows, I think you don’t need to do much on that but for Macand Linux , you need to do something.

This one is read only, so it is OK now butfor the upload you need to change to write and read here, okay, you can just change itwith the chmod 777 but there is an interface to use them.

So make sure you give the password.

Read and write and make sure you can enclosethe file that should be read and write.

Okay and close it.

Now, you are good to go.

You can upload any file to this folder.

I need to create PHP file here so I call itupload.

PHP.

You don’t need any html for that becauseit’s just for example.

So first you need to know if the image isisset() or not, post or I call the key image, if it is in here u got to do something withthat, if it’s not just echo, image not in and here if the file is in, then you wantto upload that file, and remember the file has to have a name so let’s create a name,the name has to be unique, so let’s just call id and maybe a time, a time is betterbecause time is going to be very different from one to another and you are going to usea millisecond.

So id is now minute, second and microsecond,so it’s like you get a unique id for the file.

And then you need a folder so upload folder,in this case you are going to use upload folder here, and then the image you get from thepost, remember the file here is just a string and not a real file, so you need to decodeit to a real physical file, so the file need a name, the path of that id and the file isjpeg and then you are going to need a method file_put_contents, the first one is path andthe second one is image, so remember that image has a string, you cannot make it asstring and then convert if to an image so we need decoder, simply just call base64,decode and that’s all, and you can just upload to the file.

But then you want to test if its correct ornot, and remember when you upload that, the result cannot be true, if something true isa number, because the return value of this is a number like 1 2 3, it cannot going tobe true.

So, if it has it like this, you cannot getthe result so make sure you have to do it like this.

If not false it means you uploaded it successfully.

I just call it uploaded_success and then youexit that.

Else if not upload that, maybe uploaded_failedand exit.

Okay.

That’s all.

Before you can connect from android, you haveto make sure you can connect from your emulator.

So go to home and then go here, http:// 10.

0.

3.

2if you have a port like 8080 for me I just use /news/upload.

Php and then enter.

And you can see the image is not in but atleast you can see that you can access that from emulator.

If you cannot access that I suggest that yougo to internet and you goggle something like WAMP access forbidden because I get a lotof problems from uses, from readers from YouTube so I think I have to point out, you have togo to XAMPP, you go to configure and then you go to configure file and around here makesure you have require all granted, if denied then you cannot access from your android.

One more thing, this one is that you connectfrom your android emulator, if you connect from your phone you have to provide PC I.

Paddress, on mac you can go to open network preferences and then you can see the IP address,for windows you can just go to CMD command prompt and type IP conflict and see the IPaddress and then you have to make sure that you are using the same network.

So now you are ready to work with android,connect to the PHP, so this is what I’m loading here and then I will call post responseAsync task, it’s part of the new library I just added, at this point you need threeparameters, one is application context, second one is the data, in here you have to makesure the first parameter has to be the exactly the same to this one, so if you change thisyou have to change it here also.

So now I use image, the capital and smallletter must be exactly the same.

The second one is the encoded image and it’sgoing to be a string because you encoded the file to string already and you need a resultback so you use another anonymous task and there is going to be anonymous inside anonymous.

So the IP here must be the earliest one andagain if you want to connect from your phone then you need to send this to your PC.

It must be the web server IP address not yourphone address, and the port, my own is news/upload.

Php and I just updated it on the library so youcan now handle the better so here you can adopt each exception.

It’s going to have four exception and youcan handle each one.

And then here in your process finish, youcan get the result, so remember the result is from here, from the echo here, so if yourresult is uploaded_success it means you uploaded file successfully, so if s and I am unablenot to use the equal because mostly have to have some space I just have to use containsand the word must be uploaded_success and then you can say “Image uploaded successfully”and else then “Error while uploading”.

Okay.

Let’s see.

Open the file and if you can upload it youare going to see the image in here, let’s try the camera first, upload, oops, thereis some error here, I’m not sure if this one is correct so let’s change it to mainactivity.

Ok one more time, there’s some error, Ifound the problem, and actually I forgot to add the permission, use the internet to addthe permission.

Okay.

Uploaded successfully and you can go to viewupload and see uploaded file.

One more time gallery here and yes, okay,successfully but the image you cannot see because we cannot upload the right file, butwe upload the 24 here but it is of good quality.

One more problem if we don’t see that iswhen we do the capturing, it is in portrait and when you open it it’s in landscape modeso you want to rotate like this, actually there is a library for that to do the fixing,Over here, you can go to 9re/ ExifUtil to check out about it, but because you just wantto rotate it for just capturing, so I may write my one method here, private bitmap andgetRotatedBitmap (Bitmap bitmap) and then you want to rotate it, it must be angle andthe matrix, new Matrix and matrix.

Postrotate and then you need the Bitmap and then createbitmap.

This one you need to use the second one andthe first one is the source.

It comes from here and let us call it as source.

The second one is the float which has a methodthat comes from here.

Here is another method to create a bitmap.

So we do not want to change the position.

So 0,0, source.

GetWidth(), source.

GetHeight(),matrix, return, true and now you want to change the camera, rotate the bit map to change to90 degrees.

And one more thing if you don’t have anyimage and you click on upload, there will be an error because there is no string here,so let’s check it over here: if selected photo equals null, if there is noimage you can just say no image selected, so then you want to return that, return heremeans that you exit from this even.

The last part is not necessary but for yourtesting, sometimes you want to test from your android and sometimes from your phone, sothen I want to have a test and then you can use put the IP address but the network isdifferent then the IP address is different.

I call it etIpaddress and I need to give adefault number is 10.

0.

3.

2 and then over here, You get the data and that is EditText andsomething like this and then when you upload from here, you have some changes, over herewe have IP, so I use this IP instead of number, and then I can test from my phone easier,and from my emulator easier.

So maybe if I change the position, one moretime, yes the null should go first because it is the null that exists and this is thewall line, and if I take that, this is going to run.

So the wall lying here is going to run thisbecause it is now OR, so it just exit that.

So then you need the IP and I think you cangive the IP like this, upload it, and now the last part is the same.

So, you can see now, the IP is in and thecorrect one and the final test, upload, yes and may be like this one, and okay.

That’s it for the day.

I hope you enjoy it.

If you like the video, please do not forgetto subscribe my channel.

I will see you in the next future.

Thank you!.



Thus Article Best Android Studio Tutorial on How to Upload Image/File to Server (Part 2) - Base64 and PHP

That article Best Android Studio Tutorial on How to Upload Image/File to Server (Part 2) - Base64 and PHP 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 2) - Base64 and PHP the link address http://svedda79.blogspot.com/2016/06/best-android-studio-tutorial-on-how-to.html

0 Response to "Best Android Studio Tutorial on How to Upload Image/File to Server (Part 2) - Base64 and PHP"

Post a Comment