Android Upload Image using Android Upload Service

Hello guys, in this post I came up with an easy solution for uploading files from android to server. So today we will see an example of Android Upload Image to Server. I have already posted some example of Android Upload Image to Server previously. But in this post we will use android upload service for our Android Upload Image App. You can also check the previous tutorials I posted about uploading images from android to server from the given links.

In this tutorial I will store the image file inside servers directory and in database I will store the URL of the image. For this I will use Android Upload Service library and it makes uploading files super easy. So lets see how we can do it. First we will create our server side codes.

Android Upload Image to Server Video Tutorial

You can also go through this video tutorial to learn how to upload image from android to server.

Creating Server Side Codes for Android Upload Image

The first thing we need is to create our server side web services. For server side I am using PHP and MySQL. And for this I am using Wamp server. You can still use xampp or any other application. Now follow the steps to create your web service to handle the file upload.

Go to localhost/phpmyadmin and create the following database table.

Now inside your server’s root directory (c:/wamp/www) and create a new folder. I created AndroidUploadImage.

Inside the folder create a folder named uploads, in this folder we will save all the uploaded images.

Now test your script. You can use a rest client to test it I am using POSTMAN. See the below screenshot.

If you are seeing the above response then. Your script is working fine. You can check the database and upload folder which you have created.

Database

Upload Directory

So its working absolutely fine. Now lets move ahead and create a android project.

Creating Android Studio Project

We are done with the server side coding for this Android Upload Image Example. Now lets code our android application. Follow the steps given.

Create a Android Studio Project.

Create a class named Constants.java and write the following code. The following class contains the path to our php file which we created. You are seeing two strings. The second it the path to the file we will create at the end of this post.

So thats it for this Android Upload Image tutorial friends. You can also use this method to upload video and any type of file to your server. Leave your comments if having any doubts or feedbacks. Thank You 🙂

About Belal Khan

Comments

Assala-mualikum, This is a best tutorial for upload image(base64) to server using volley library. Its done well. Now I want to upload pdf to use base64——– I want know is that possible to upload pdf base64?? ………… If possible I request you to upload a tutorial for pdf upload. Thank you for your great tutorial. Happy Ramadan…

Amazing tutorial guys It came exactly when I needed it to build upload for our app .. Fliiby << 🙂 will be live from July I hope 🙂
I did a lot of modifications like loading images with glide since loading bitmap is memory heavy.. text immediately started lagging when I load 4-5mb picture.. changed notifications system etc… but guys thanks again it helped me a lot..

Hi Belal,
I am following your tutorials and these are very helpful for me, Thank you for tutorials.
I want the code for downloading pdf file which is stored in my database.
Could you please write the code and upload it for me.

Belal Khan your tutorial is fantastic. It is easy to comprehend and implement. However I am getting this error in the error log in the server
“mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /…………../AndroidUploadImage/upload.php on line 78”

I am using postman exactly as you show and I am getting no responce and status 200 OK.

What could be the issue here?
Thanks in advance.

PS. In your tutorial you use AndroidUploadImage, but in your code you use AndroidImageUpload. Some people will scratch their head if they copy the code and find out it doesn’t work.

I had the same error and what i did is to copy any image that you have and paste it in that folder and name it with the previous id, for example if your last register in database is 12 that image have to have that number, and now try to do upload again a picture using the app and it might work.

Everything is working well for me, except one thing. supposing someone chooses not to select an image for upload, but clicks the upload button, the app crashhes! How do I prevent the app from crashing? prompting the user “please select an image”

/*
We are generating the file name
so this method will return a file name for the image to be upload
*/
function getFileName(){
$sql = “SELECT max(notesID) as notesID FROM notes”;
$result = mysqli_fetch_array(mysqli_query($con,$sql));

I followed your tutorial and uploads are going through(even a 5 mb image goes through successfully. However, when i uppload another image, it overwrites the previous image. The first image was uploaded as 1.jpg. Then I uploaded a .png file and it was saved as 1.png. But when I try uploading more jpg files, it only overwrites and does not increment the number to 2 and so on. I also noticed my table returns no rows after this. Any help is appreciated. Thanks.

I appreciate your tutorial so much and it has helped me to build a good android app which I will be asking you to download and assess for improvement.

I an using this example to upload an image but I don’t want the image to be displayed on the activity but the filepath to be set on the edittext. In the process, I realised that you are not setting the filepath on the edittext.

Hi,
Belal sir ,i am using image upload but some error to solution is tough in ,i am copy our website but no solution please help image upload in android studio

Warning: move_uploaded_file() [function.move-uploaded-file]: It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Asia/Calcutta’ for ‘IST/5.0/no DST’ instead in/home/celeadmn/public_html/demo/upload.php on line31

Warning: move_uploaded_file(uploads/12.html) [function.move-uploaded-file]: failed to open stream: No such file or directory in/home/celeadmn/public_html/demo/upload.php on line31

Warning: move_uploaded_file() [function.move-uploaded-file]: It is not safe to rely on the system’s timezone settings. You are *required* to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected ‘Asia/Calcutta’ for ‘IST/5.0/no DST’ instead in/home/celeadmn/public_html/demo/upload.php on line31

Sir thank u so much, only you made it possible,
i tried number of methods tutorials but no one helped and you just wrote the code in such a simplified manner that beginners like us can understand very easily moreover you made this possible to upload the images to server otherwise many of us were in trouble to how to upload images to server i will follow you everywhere nowonwards , such a great help to me for making my minor project .
thank u so much sir!

Hello Sir, I have an error in this code is if we run this without validation means if we not select any file then it asking please select file and data cannot be submitted but i want to make a choice for the user that if user wants to select image or if user not want then it skip but the data will be submitted but this tsk is not performed..

Thank you the tutorial but I am having some trouble getting it running. When I hit upload, the app crashes. After some inspection, it seems the cursor in getPath() is empty. This is the redefinition of the cursor that is empty, so the cursor defined as: cursor = getContentResolver().query(
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
null, MediaStore.Images.Media._ID + ” = ? “, new String[]{document_id}, null);

I followed every step very carefully around 5-7 times, but not able to upload image. I have Ubuntu 16 server. I tried with Postman that is also not working for me. I really need help.
In Php file If I write print_r($_POST); it show everything is perfect in $_POST, but when we get $_POST[‘name’] and other values, they comes as empty. Means print_r($_POST[‘name’]); prints nothing.

Hi bro Thanks I follow your tutorials it help alot and I learn more things and coding techniques I have one question if you can can make tutorials on it its very important and helpful for others
My question:
I have follow this tutorial and I have done my task but I want to run this application on my android mobile and I send all data from Mobile to laptop, Laptop use as server I am use wamp so is that possible, if so please make tutorial and show to send run same application on mobile and all data send from mobile to laptop instead of emulator using Wifi connection

Hello, nice article but i faced problem when use setNotificationConfig the error like (android.app.RemoteServiceException: Bad notification for startForeground: java.lang.RuntimeException: invalid channel for service notification). How to fix it please…

Great job sir. It is too much helpful. Now my next step is to display the data in another activity. As the user click on the item from recyclerview all the data of selected item must be displayed in other activity. I only show Name and Image in the recyclerview. and want to display all of its data in other activity. How to do that?

Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here:
Cookie Policy

About Simplified Coding

Simplified Coding is a blog for all the students learning programming. We are providing various tutorials related to programming and application development. You can get various nice and simplified tutorials related to programming, app development, graphics designing and animation. We are trying to make these things simplified and entertaining. We are writing text tutorial and creating video and visual tutorials as well. You can check about the admin of the blog here and check out our sitemap