Upload Image From Gallery To PHP Server In Android Studio Example

Hello, all. Welcome to upload image from gallery in Android Studio example.

In upload image from gallery in Android tutorial, learn how to choose image from gallery and then send or upload it to PHP-MySQL server.

First, we will upload image to server then we will fetch this uploaded image into ImageView using AQuery Library.

You will get the professional format to call remote Web Services in proper and easiest way at the end of upload image from gallery in Android example.

If you want to capture image from camera then refer this: upload image from camera in android.

First, check output of upload image from gallery in Android example, then we will develop it.

How to get maximum benefit from upload image from gallery in Android Studio example

Download Source Code

Try Our App

Download DemoNuts app to learn anytime, anywhere: Click to get Demonuts App

Or search “demonuts” in play store from android mobile device.

Creating Upload Image From Gallery In Android Studio Tutorial step by step

If you are using adblocker, then you will not be able to show our site smoothly. For example, you find step 6 after step 1 or similar to this. So please disable adblocker to fully utilize all resources of DemoNuts

Developing PHP Script

Make a new PHP file named “config.php” and copy below

Create a new PHP file and name it “uploadfile.php” and add below source code

I have a folder named “uploadedFiles,” in which all uploaded images will be saved.

You need to change this line as per your folder directory

Step 1: Create a new project in Android Studio.

Step 2: Updating AndroidManifest.xml file

 Add required permissions between <manifest>….</manifest> tag.

Note: If you are targeting SDK version above 22 (Above Lollipop)  then you need to ask a user for granting runtime permissions. Check marshmallow runtime permission for more information.

Final code for AndroidManifest.xml file

Step 3: Updating build.gradle(Project: project_name) file:

Add the following

in the below structure.

So final code for build.gradle(Project: project_name)  will look like this:

Step 4: Updating build.gradle(Module:app) file

Add following code into dependencies{} 

Add below code

in the parent, android{} as below

Final source code for build.gradle(Module:app) will be:

Step 5: Adding common classes

We will add some common classes which contain constant variables and public methods.

We can use these variables and methods anywhere in the whole project so that it will reduce data redundancy.

Means that we will write them only once and then we can use them anytime and anywhere when needed.

Names of the classes are:

  1. AndyUtils
  2. AsyncTaskCompleteListener (Interface)
  3. MultiPartRequester
  4. ParseContent

Step 6: Creating AndyUtils

Create a Java class named AndyUtils and add below source code

This class contains a methods to show(showSimplrProgressDialog()) and remove(removeSimpleProgressDialog()) progress dialog when app is fetching JSON data from server.

AndyUtils also includes a method (isNetworkAvailable()) to check whether the Internet of Android device is on or off.

Step 7: Creating AsyncTaskCompleteListener Interface

Prepare new interface named AsyncTaskCompleteListener and add following source code

Step 8: Creating MultiPartRequester

Create a new Java class named “MultiPartRequester” and add below

We will use methods of this class to establish a connection between an Android device and web server.

Step 9: Creating  ParseContent

Open new Java class and give it a name ParseContent, then Add below source code

In above source code, isSuccess(String response)  method is used to check whether a status of response is true or false.

getErrorCode(String responsemethod is used to get the message of JSON data.

getURL(String response) method will parse JSON data.

If you want to learn how to parse JSON, then refer this example: JSON Parsing In Android

Step 10: Updating activity_main.xml file

 Copy and paste below source code in activity_main.xml file

Step 11: Preparing MainActivity.java class

Add following source code in MainActivity.java class

Step 12: Description of MainActivity.java

MainActivity implements AsynTaskCompleteListener interface. So we will have to override onTaskCompleted() method.

When the button is clicked, Gallery intent will be opened as following.

When user selects image, compiler comes to onActivityResult() method as below.

Here, image is saved and uploaded to server.

saveImage(bitmap)  method will save the image as below.

In above code, IMAGE_DIRECTORY is the folder name, in which images will be saved.

uploadImageToServer() method will call Web Service.

From here, compiler will go to onTaskCompleted() method.

The following line will fetch image from URL.

How to get exact Android example while searching in Google

Click to get Important Google searching tips

So enough for upload image from gallery in android example.

Feel free to comment your queries and reviews in below comment section. Thank you 🙂

Hardik Parsania

I am mobile app developer. I am always passionate about solving complex and real world problems.I have developed many apps with great interest and superior logical skills.

You may also like...

5 Responses

  1. kyd says:

    plz help me

    java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String java.lang.String.toString()’ on a null object reference

    • Hardik Parsania says:

      your edittext or textview or some other view is null. checkout this first.

      • kyd says:

        I’m php log

        [Thu May 04 11:11:28.423578 2017] [:error] [pid 27871] [client] PHP Warning: include_once(config.php): failed to open stream: No such file or directory in /var/www/html/uploadfile.php on line 5
        [Thu May 04 11:11:28.423701 2017] [:error] [pid 27871] [client] PHP Warning: include_once(): Failed opening ‘config.php’ for inclusion (include_path=’.:/usr/share/php’) in /var/www/html/uploadfile.php on line 5
        [Thu May 04 11:11:28.423816 2017] [:error] [pid 27871] [client] PHP Notice: Undefined variable: con in /var/www/html/uploadfile.php on line 17
        [Thu May 04 11:11:28.423871 2017] [:error] [pid 27871] [client] PHP Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /var/www/html/uploadfile.php on line 17

        help me plz


    • AKM says:

      try to select image from other options like File Explorer.

  2. Hardik Parsania says:

    You have error in directory structure. Make sure that you have added required files at respective directories.

Leave a Reply

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