Tuesday 15 October 2013

Image upload

When i want to upload image into web server 
 GetNameValue nv = new GetNameValue();
String signupop = chngpassop
                        .getOPName(Utility.OPERATION_NAME);


List<NameValuePair> nvp = nv.getMyInfoEditDetail(userfname,
                        userlname, usermobile, userhomephone, userworkphone,
                        userdob, strheight, strwight, streyecolor,
                        strhaircolor, strtimepref, selectedImagePath, user_id,useremail);
                if(!TextUtils.isEmpty(selectedImagePath)){
                    result = httpPost.uploadUserImage(
Utility.SIGNUP_PAGE
                            + signupop, nvp);
                }else{
                    result = httpPost.post(
Utility.SIGNUP_PAGE
                            + signupop, nvp);
                }


my http post class having function for upload image to server as define below

public String uploadUserImage(String op,List<NameValuePair> nvp)
    {
        try
        {
            HttpConnectionParams.setConnectionTimeout(params,TIMEOUT_MILLISEC);
            HttpConnectionParams.setSoTimeout(params,TIMEOUT_MILLISEC);
          
                HttpContext localContext = new BasicHttpContext();
               // System.out.println("Server Link: "+serverurl+op);
                post = new HttpPost(serverurl+op);

                try {
                    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);

                    //System.out.println("Upload Image: "+nvp);
                    if(nvp != null)
                    {
                         for(int index=0; index < nvp.size(); index++)
                         {
                             if(nvp.get(index).getName().equalsIgnoreCase("user_photo"))
                            {
                                if(nvp.get(index).getName().equalsIgnoreCase("user_photo"))
                                {
                                  // If the key equals to "U_PHOTO", we use FileBody to transfer the data
                                    multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
                                }
                                else
                                {
                                    // Normal string data
                                    multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
                                    }
                                 }
                             else
                             {
                                if(nvp.get(index).getName().equalsIgnoreCase("user_photo"+index))
                                {
                                    //System.out.println("---In if UMI_NAME---");
                                    multipartEntity.addPart(nvp.get(index).getName(), new FileBody(new File (nvp.get(index).getValue())));
                                }
                                else
                                {
                                    // Normal string data
                                    //System.out.println("---In else UMI_NAME---");
                                     multipartEntity.addPart(nvp.get(index).getName(), new StringBody(nvp.get(index).getValue()));
                                }
                             }
                         }
                    }
                    post.setEntity(multipartEntity);
                    response = client.execute(post, localContext);
                    entity = response.getEntity();
                    if(entity != null)
                    {
                        result = EntityUtils.toString(entity);
                    }
                    else
                    {
                        Log.d("Response Failed","Response from server is failed");
                    }
                  
                } catch (IOException e) {
                    e.printStackTrace();
                }

        }catch(Exception ex)
        {
            ex.printStackTrace();
        }
        return result;
      
    }

No comments:

Post a Comment

Comments

Find Hours Diffrence in Kotlin

  In Kotlin, determining the difference in hours between two timestamps is a common task, especially in scenarios involving time-based calcu...