Showing posts with label Image upload in android. Show all posts
Showing posts with label Image upload in android. Show all posts

Monday 8 October 2012

Multipart image upload in android

Hello Friends,

I'm trying to upload image to my server in Android..???

After spending a lots of time , I came across Multipart image Upload.Using this 
we can easily upload image as well as file on our server.
Note: The method upload the image using http multipart form data.
           In my condition , My web web service method(upload_image) requires
           three parameter .
            1. A text message
            2. Image or Template Id
            3. Icon or Image in byte array format with filename.
   Also , I have to pass user token which i am saving into share preference,
   when user successfully logged in. Also the API Version which is nothing
   but a string(e.g 1.1) which will be saved in my Config Class.
   Please , Change the above parameter and post url as per your requirement. 
           
Note: Here , Instead of using android default HTTP client , I am using the 
          Apache Http client. Download it from  Here


     And placed it inside your project lib folder.


      /**
  * Method uploads the image using HTTP Multipart form data.
  * 
  * @param imageData
  * @param filename
         * @param icon   
  * @return
  * @throws Exception
  */



public static boolean uploadImage(final byte[] imageData, String filename ,String message) throws Exception{

        String responseString = null;       

        PostMethod method;

        String auth_token = Preference.getAuthToken(mContext);


        method = new PostMethod("http://10.0.2.20/"+ "upload_image/" +Config.getApiVersion()
               + "/"     +auth_token); 

                org.apache.commons.httpclient.HttpClient client = new              
                                            org.apache.commons.httpclient.HttpClient();

                client.getHttpConnectionManager().getParams().setConnectionTimeout(

                                100000);

                FilePart photo = new FilePart("icon", 
                                                      new ByteArrayPartSource( filename, imageData));

                photo.setContentType("image/png");
                photo.setCharSet(null);
                String s    =   new String(imageData);
               Part[] parts = {
                                new StringPart("message_text", message),
                                new StringPart("template_id","1"),
                                photo
                                };

                method.setRequestEntity(new 
                                              MultipartRequestEntity(parts, method.getParams()));
                client.executeMethod(method);
                responseString = method.getResponseBodyAsString();
                method.releaseConnection();

                Log.e("httpPost", "Response status: " + responseString);

        if (responseString.equals("SUCCESS")) {
                return true;
        } else {
                return false;
        }
    } 


Hope This Will Helps Some one.
Cheers :)
Enjoy Coding... :)

 

Copyright @ 2013 Android Developers Blog.