Upload Image From Camera In Android Studio To PHP Server Example

Hello, Geeks. You are at upload image from camera in Android Studio example.

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

First, we will upload image to server then we will fetch or load 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 camera in Android example.

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

If you want to select image from gallery then refer this: upload image from gallery in android.

Download Source Code

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

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 “uploadedFiles,” in which all uploaded images will be saved.

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, Camera intent will be opened as following.

When user captures the 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.

So enough for upload image from camera 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...

16 Responses

  1. Gautham says:

    config.php Missing!!!

  2. Lloyd says:

    09-01 17:21:17.283 4136-4136/com.example.lar_pc.bottomnavdemo E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.lar_pc.bottomnavdemo, PID: 4136
    java.lang.NullPointerException: Attempt to invoke virtual method ‘java.lang.String java.lang.String.toString()’ on a null object reference
    at com.example.lar_pc.bottomnavdemo.AlbumsFragment.onTaskCompleted(AlbumsFragment.java:103)
    at com.example.lar_pc.bottomnavdemo.MultiPartRequester$AsyncHttpRequest.onPostExecute(MultiPartRequester.java:119)
    at com.example.lar_pc.bottomnavdemo.MultiPartRequester$AsyncHttpRequest.onPostExecute(MultiPartRequester.java:52)
    at android.os.AsyncTask.finish(AsyncTask.java:674)
    at android.os.AsyncTask.-wrap1(AsyncTask.java)
    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:691)
    at android.os.Handler.dispatchMessage(Handler.java:111)
    at android.os.Looper.loop(Looper.java:207)
    at android.app.ActivityThread.main(ActivityThread.java:5728)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)

    ——— beginning of main

  3. adolf schulz says:


  4. Srinath says:

    How can auto select camera at lower resolution in programmatically then capture the image?

  5. Ony 7 says:

    Hey Dude, Great tutorial,
    how to do this on a fragment?

  6. Birhanu says:

    Hy Guys why you not upload the complete source code???

  7. oualid says:

    hi ,
    thank you for this amazing tutorial .
    i try this but the resolution of the picture in server is too low, how can i show it in origine resolution

    • Hardik Parsania says:

      Below line in saveImage() method is compressing image.

      myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

      Remove this line to upload original image.

  8. Ganesh says:

    how to pass the intent from MultiPartRequester to MainActivity

    • Hardik Parsania says:

      It is weird to pass intent from Multipart. This structure gives you response in MainActivity’s in onTaskComplete method. so try to handle situation with this structure.

  9. Tri says:

    Thank you very much for the knowledge.

Leave a Reply

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