hpsaturn

Image Upload Android

Mar 14th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.35 KB | None | 0 0
  1. public String uploadFile(){
  2.     if (DEBUG)Log.d(TAG, "onActivityResult: data==null");
  3.     try {
  4.    
  5.         Uri outputFileUri = Uri.fromFile(file);
  6.         if(DEBUG)Log.d(TAG,"outputFileUri post: "+outputFileUri);
  7.                
  8.         HttpClient imgClient = new DefaultHttpClient();
  9.         imgClient.getParams().setParameter("http.connection.timeout",Integer.valueOf(1000));
  10.         if(DEBUG)Log.d(TAG,"Service Image Upload: "+url);
  11.         HttpPost imagePost = new HttpPost(url);
  12.  
  13.         BasicHttpEntity imgEntity = new BasicHttpEntity();
  14.         imgEntity.setContent(new FileInputStream(file));
  15.         imagePost.setEntity(imgEntity);
  16.         imagePost.setHeader("imageName", "image");
  17.         imagePost.setHeader("imageExtension", "jpg");
  18.            
  19.         HttpResponse imgResponse = imgClient.execute(imagePost);
  20.         if (imgResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
  21.             HttpEntity httpEntity = imgResponse.getEntity();
  22.             String response = EntityUtils.toString(httpEntity,HTTP.UTF_8);
  23.             if (DEBUG)Log.d(TAG,"imgUpload response:"+response );
  24.             return response;
  25.         }else{
  26.             if (DEBUG)Log.d(TAG,"imgUpload response: FAIL");
  27.             return null;
  28.         }
  29.         } catch (ClientProtocolException e) {
  30.             if (DEBUG)Log.d(TAG, "ClientProtocolException:");
  31.             e.printStackTrace();
  32.             return null;
  33.         } catch (IOException e) {
  34.             if (DEBUG)Log.d(TAG, "IOException:");
  35.             e.printStackTrace();
  36.             return null;
  37.         }
  38.  
  39.     }
  40.                
  41. }
Advertisement
Add Comment
Please, Sign In to add comment