2014-09-22 156 views
0

我需要在我的第一个由Eclipse开发的android应用程序中,将android文件上传到远程服务器。在Android上传文件到服务器

为此,我将在asp网络中使用一个web服务。

在我的android xml表单中,我不明白如何从图库中选择一张照片并从智能手机发送图片。

我将不胜感激任何帮助,您可以给我在工作的这个问题。

预先感谢您。

回答

0

http://lakjeewa.blogspot.in/2012/03/simple-android-application-to-send-file.html

一次请参阅本TUTS这将是有益的给你

请投票支持它,它是有用的

+0

谢谢你,但我需要在我的画廊的智能手机选择图像。 ..在你的例子中选择文件上传? – 2014-09-22 14:50:13

+0

ok.i会找到你的答案 – 2014-09-22 14:51:33

+0

http://androidmyway.wordpress.com/2012/02/05/selecting-image-from-gallery-or-taking-image-from-camera-with-options-menu-上传到服务器/ – 2014-09-22 14:54:14

0

使用多部分实体上传照片。

HttpClient httpclient = new DefaultHttpClient(); 
     HttpEntity httpEntity = null; 
     HttpResponse response = null; 

     photoHttpPost = new HttpPost(ADD_PHOTO_URL); 


     try { 
      String filePath = image;//imagepath here 

      MultipartEntity entity = new MultipartEntity(); 
      entity.addPart("refuges_id", new StringBody("" + refugeId)); 

      List<NameValuePair> parameters = new ArrayList<NameValuePair>(0); 

      parameters.add(new BasicNameValuePair("image", filePath)); 

      for (int index = 0; index < parameters.size(); index++) { 
       if (parameters.get(index).getName().equalsIgnoreCase("image")) { 
        // If the key equals to "image", we use FileBody to transfer 
        // the data 
        entity.addPart(parameters.get(index).getName(), 
          new FileBody(new File(parameters.get(index) 
            .getValue()))); 
       } 
      } 

      photoHttpPost.setEntity(entity); 
     } catch (Exception e) { 
      e.printStackTrace(); 
     } 

     // Execute HTTP post Request 
     try { 
      HttpParams httpParameters = new BasicHttpParams(); 
      HttpConnectionParams.setConnectionTimeout(httpParameters, 
        timeoutConnection); 
      HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket); 
      httpclient = new DefaultHttpClient(httpParameters); 
      response = httpclient.execute(photoHttpPost); 
      httpEntity = response.getEntity(); 
     } catch (ConnectTimeoutException e) { 
      e.printStackTrace(); 
      result = "timeout"; 
     } 

     // Execute HTTP Get Request 
     if (httpEntity != null) { 
      InputStream is; 
      try { 
       is = httpEntity.getContent(); 

       BufferedReader reader = new BufferedReader(
         new InputStreamReader(is, "UTF-8"), 8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 

       while ((line = reader.readLine()) != null) { 
        sb.append(line + "\n"); 
       } 
       is.close(); 
       result = sb.toString(); 

      } catch (IllegalStateException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     }