Android: Upload image via HTTP with a POST method

We want to send an image which is stored on the local storage device. We want to send it from a Java class to a remote site via HTTP. This must work for Android as well.

Basically what we need is this:
1) A PHP script that handles an image received via POST
2) A Java class that sends this image from the local storage via HTTP

1) The PHP script uses the ImageUtil class in order to manage the uploaded file. You can download it: ImageUtil.php.

It goes like this:

if ($_FILES['userfile']['name']){
     $name = "desiredImageName";
          // move file to needed location
          if (ImageUtil::uploadImage($_FILES['userfile'], $name))
               echo "Image uploaded: [*".$name."*]";
               echo "Failed to upload image!";
     } catch (InvalidExtensionException $e){
           echo "Caught exception: ",  $e->getMessage(), "\n";
     } catch (InvalidDimensionsException $e){
           echo "Caught exception: ",  $e->getMessage(), "\n";
     } catch (InvalidDimensionsException $e){
           echo "Caught exception: ",  $e->getMessage(), "\n";
     } catch (InvalidSizeException $e){
           echo "Caught exception: ",  $e->getMessage(), "\n";

2) This Java class was found on StackOverflow and was written by Piotr Kochański. It uses the Apache HttpComponents libraries; current version is 4.1.1. You can download here.


import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.ContentBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreProtocolPNames;
import org.apache.http.util.EntityUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpVersion;

public class SendImageHttpPost {

          public static void sendPost(String url, String imagePath) throws IOException, ClientProtocolException  {
                    HttpClient httpclient = new DefaultHttpClient();
                    httpclient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);

                    HttpPost httppost = new HttpPost(url);
                    File file = new File(imagePath);

                    MultipartEntity mpEntity = new MultipartEntity();
                    ContentBody cbFile = new FileBody(file, "image/jpeg");
                    mpEntity.addPart("userfile", cbFile);

                    Log.e("executing request " + httppost.getRequestLine());
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity resEntity = response.getEntity();
                    if (resEntity != null) {
                    if (resEntity != null) {

You can download this class:


2 thoughts on “Android: Upload image via HTTP with a POST method

Add yours

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

Blog at

Up ↑

%d bloggers like this: