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']){
     include("ImageUtil.php");
     $name = "desiredImageName";
     try{
          // move file to needed location
          if (ImageUtil::uploadImage($_FILES['userfile'], $name))
               echo "Image uploaded: [*".$name."*]";
          else
               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 java.io.File;
import java.io.IOException;

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

                    httppost.setEntity(mpEntity);
                    Log.e("executing request " + httppost.getRequestLine());
                    HttpResponse response = httpclient.execute(httppost);
                    HttpEntity resEntity = response.getEntity();
                    Log.e(""+response.getStatusLine());
                    if (resEntity != null) {
                              Log.e(EntityUtils.toString(resEntity));
                    }
                    if (resEntity != null) {
                              resEntity.consumeContent();
                    }
                    httpclient.getConnectionManager().shutdown();
          }
}

You can download this class: SendImageHttpPost.java.

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

Add yours

Leave a comment

Blog at WordPress.com.

Up ↑