Home > Android > Android: Upload image via HTTP with a POST method

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.

About these ads
Categories: Android
  1. Babul
    March 24, 2014 at 9:57 am

    Hahhh… Man… You finally made my day… Dude…
    Many Thanks to you from bottom of my Heart

    Like

  1. No trackbacks yet.

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s

Follow

Get every new post delivered to your Inbox.

%d bloggers like this: