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.

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.

android upload image database.jpg

  • 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.
  • Create a file named dbDetails.php and write the following code.

  • Now create a file named upload.php and write the following code.

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

android upload image web service

  • 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.
android upload image database

Database

android upload image 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.

  • You need to change the IP according to your system. To know the IP you can use IPCONFIG command in command prompt (windows user).
  • Now we need to add android upload service to our project.

Adding Android Upload Service

  • Go to your app level build.gradle file and add the following line inside dependencies block and sync your project.

  • Come to activity_main.xml and write the following xml code.

  • The above code will generate the following layout.

android upload image example

  • As you can see we have two buttons, one to select image and other to upload image. We also have an EditText to enter the name for the image.
  • Now come to MainActivity.java and write the following code.

  • Finally add the storage and internet permission on your manifest.

  • Thats it now just run your app.

android upload image example app

  • Bingo! Its working absolutely fine. Now you may need to fetch the uploaded images.

Fetching the Uploaded Images

  • To fetch the images inside your server side project, create one more file named getImages.php and write the following code.

  • This code will give you the following JSON.

  • Now you can use the following tutorial to display the images using the above JSON.

Android Custom GridView with Images and Texts using Volley

  • You can also get my source code from the link given below.

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 🙂

Subscribe To Our Newsletter

Subscribe To Our Newsletter

Join our mailing list to receive the latest news and updates from our team.

You have Successfully Subscribed!

Comments

  1. Mahmudur Rahman says

    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…

  2. pavithra says

    “Oops you just denied the permission” this message is displaying,images are not uploading,how to slove this.

  3. Ari says

    nice article (y)
    sir,, can i request tutorial about how to implement Single Sign-On Android??
    thank u sir 🙂

  4. nemanja says

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

    ==glide change for image=====

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
    filePath = data.getData();

    Glide.with(getApplicationContext())
    .load(filePath)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    // .fitCenter()
    .centerCrop()
    .crossFade(500)
    .priority(Priority.HIGH)
    .into(imageView);

    }
    }

  5. Avinash Yadav says

    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.

  6. Kamla Sharma says

    How to upload the image that is captured with camera within the android application,, im getting trouble please help….

  7. Paul Peridis says

    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.

  8. jay says

    name and url are uploaded in mysql. but image is not uploading in the folder when i run the php code in postman.

  9. CK says

    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”

  10. Andro CG says

    Error in line 79 :
    Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in C:\wamp\www\phpcode\upload.php on line 79

  11. tao says

    i can ‘t get the file path from the gallery application keeps crashing when i i try to upload . Error on the console shows that the method getpath() is not returning the image

  12. August says

    Hello Belal. I got error Please choose a file in Postman. Please help

    getMessage();
    }
    //displaying the response
    echo json_encode($response);

    //closing the connection
    mysqli_close($con);
    } else {
    echo $response[‘error’]=true;
    echo $response[‘message’]=’Please choose a file’;
    }
    }

    /*
    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));

    mysqli_close($con);
    if($result[‘notesID’]==null)
    return 1;
    else
    return ++$result[‘notesID’];
    }
    ?>

  13. AbeerAlRumaidh says

    I tried the php code in POSTMAN and everything is fine and i tried the code in android studio and it is working fine but once i upload the image, i get this “Error during upload”. please help

  14. Neha Abdulla says

    I used the same method for uploading image into filezilla server. Everything is fine but the image is not uploading into the folder created in filezilla. can you please help me?

  15. Rakesh says

    Hello Bilal,

    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.

    • Akhil says

      Hi Rakesh
      I am too Stuck on This Do You find the Solutions What Wrong here….Do you have any notions how to calculate response from server in the activity

  16. Edwin Adeola says

    Hi Mr. Belal!

    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.

    Kindly check you observation and respond.

    Thanks and regards.

  17. Rahul Manjinder says

    Thanks Bro You are my saviour.
    and once again thank u very very very very very very very very very very very very much

  18. Avinash Singh says

    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 line
    31

    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 line
    31

    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 line
    31

    Warning: move_uploaded_file() [
    function.move-uploaded-file]: Unable to move ‘/tmp/phpUbekSp’ to ‘uploads/12.html’ in
    /home/celeadmn/public_html/demo/upload.php on line
    31

    {“error”:false,”url”:”http:\/\/www.celebritiesacademy.com\/Androidimageupload\/uploads\/12.html”,”name”:”aafhyh”}

Leave a Reply

Your email address will not be published. Required fields are marked *


*