Android: getting image from Camera and uploading to web server multipart connection type

 

The camera is the powerful tool and most Android-powered devices already have at least one camera installed. After sparing some time from taking selfies (most important use of camera ever known), we can also use it for many other purposes.

From a seller(E-commerce) point of view, he can add a new product from his application by adding product details and beautiful pictures taken from his camera.

In this post, we are going to take images from the camera and will upload to the PHP web server using multipart connection type.

We will use an existing camera application (that is bundled with all android devices having a camera).

Default  camera app itself gives us the ability to review/retake the image, and once an image is accepted.


Step 1: Request Camera Permission

to allow only those devices which contain built-in camera functionality.

As we know from Marshmallow 6.0 Permission policies are changed thus Permissions are requested on runtime.

 


Step2 : Take a Photo with the Camera App

Action “android.provider.MediaStore.ACTION_IMAGE_CAPTURE”  is used to invoke the native Camera of the device.


Step3: Getting result back in activity and displaying an image taken by the camera.

Now We will Override the onActivitityResult 

We will receive a Bitmap of data that we store in a temporary file. This temporary file is uploaded afterwards.

 

Notice that the image returned by the camera application is passed as an extra named ‘data’ through to the intent of the calling activity. This ‘data’ is a Bitmap object.


Step4: Uploading File to Php server

Hot Served uploadFileToServerTask uploads the file to PHP server using multipart connection type.

 

 

For Further Reading:

http://developer.android.com/reference/android/hardware/Camera.html

http://developer.android.com/training/camera/photobasics.html#TaskCaptureIntent

. . .

Comments (9)

Add Your Comment

  • Diego R FG
    source code please
    • Shubham Agarwal
      Please check edits. We have updated the blog with latest source code.
  • DC
    Can you make an example where:
    1) You start the camera
    2) Take a picture
    3) The picture is saved to the gallery
    4) The image is uploaded to the server
  • Rahul Shetty
    Sorry for the silly question, where do i find edits
  • Marco Antonio Siqueiros Macias
    and server.php? where is the code in server side?
  • Wilmer
    Hi Shubham..thanks for this great article. I am trying to implement it using a surfaceView preview to capture the image without starting intent to open the camera app…can you help me with it..thanks.
  • davin Butler
    Any chance you post the php code you are uploading to?
  • cho leung kwong
    I plan to help blinds by converting photo to text and using screen reader to speak out the text.
    1. CaptionBot works well ,but it has an upload picture button , and how to solve it for Android phone automatic uploading captured photo.
    2. A great deal is for your helping to the blind.
    3. Thanks, Jesus’ s love.
  • css.php
    Hire Us!
    Brief us about your requirements and we'll get back to you.
    Woo! Hooy!
    We have just recieved your project brief and our expert will contact you shortly.
    Send Again
    Close