2014-09-22 167 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(); 
      } 
     }