2010-09-27 105 views
2

你好,我是新的android系統開發芻議 我想知道如何上傳Android中 圖像我不找到此 任何有用的教程ü可以給我一些指導,請,幫助我。Android的圖片上傳

+0

你能更具體地說明你想用這張圖片做什麼嗎? 有很多方法來處理圖像,我需要選擇一個最適合您的需求。隨着最好的, – Patrick 2010-09-27 06:28:00

+0

所有你想要的是[這裏](http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/) 。 – Vikas 2011-01-29 09:49:16

回答

5

我建立了這個律方法適合您:

private boolean handlePicture(String filePath, String mimeType) {  
    HttpURLConnection connection = null; 
    DataOutputStream outStream = null; 
    DataInputStream inStream = null; 

    String lineEnd = "\r\n"; 
    String twoHyphens = "--"; 
    String boundary = "*****"; 

    int bytesRead, bytesAvailable, bufferSize; 

    byte[] buffer; 

    int maxBufferSize = 1*1024*1024; 

    String urlString = "http://www.yourwebserver.com/youruploadscript.php"; 

    try { 
     FileInputStream fileInputStream = null; 
     try { 
      fileInputStream = new FileInputStream(new File(filePath)); 
     } catch(FileNotFoundException e) { } 
     URL url = new URL(urlString); 
     connection = (HttpURLConnection) url.openConnection(); 
     connection.setDoInput(true); 
     connection.setDoOutput(true); 
     connection.setUseCaches(false); 

     connection.setRequestMethod("POST"); 
     connection.setRequestProperty("Connection", "Keep-Alive"); 
     connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);    

     outStream = new DataOutputStream(connection.getOutputStream()); 

     outStream.writeBytes(addParam("someparam", "content of some param", twoHyphens, boundary, lineEnd));     

     outStream.writeBytes(twoHyphens + boundary + lineEnd); 
     outStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"" + filePath +"\"" + lineEnd + "Content-Type: " + mimeType + lineEnd + "Content-Transfer-Encoding: binary" + lineEnd);   
     outStream.writeBytes(lineEnd); 

     bytesAvailable = fileInputStream.available(); 
     bufferSize = Math.min(bytesAvailable, maxBufferSize); 
     buffer = new byte[bufferSize]; 

     bytesRead = fileInputStream.read(buffer, 0, bufferSize); 

      while (bytesRead > 0) { 
       outStream.write(buffer, 0, bufferSize); 
      bytesAvailable = fileInputStream.available(); 
      bufferSize = Math.min(bytesAvailable, maxBufferSize); 
      bytesRead = fileInputStream.read(buffer, 0, bufferSize); 
     } 

      outStream.writeBytes(lineEnd); 
      outStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); 

     fileInputStream.close(); 
     outStream.flush(); 
     outStream.close(); 
    } catch (MalformedURLException e) { 
     Log.e("DEBUG", "[MalformedURLException while sending a picture]"); 
    } catch (IOException e) { 
     Log.e("DEBUG", "[IOException while sending a picture]"); 
    } 

    try { 
      inStream = new DataInputStream(connection.getInputStream()); 
      String str; 

      while ((str = inStream.readLine()) != null) { 
       if(str=="1") { 
        return true; 
       } else { 
        return false; 
       } 
      } 
      inStream.close(); 
     } catch (IOException e){ 
      Log.e("DEBUG", "[IOException while sending a picture and receiving the response]"); 
     } 
    return false; 
} 

private String addParam(String key, String value, String twoHyphens, String boundary, String lineEnd) { 
     return twoHyphens + boundary + lineEnd + "Content-Disposition: form-data; name=\"" + key + "\"" + lineEnd + lineEnd + value + lineEnd; 
} 

應該工作至今。在你的網絡服務器上,你需要一些PHP腳本,它返回一個「1」表示成功上傳,另一個表示錯誤。我還建議在ASyncTask中這樣做,以防止在上傳過程中阻止用戶。 在網絡服務器端你有一個名爲「uploadedfile」的文件。希望有所幫助!

+0

非常感謝你 – bindal 2010-09-27 06:56:13

+0

沒問題:)讓我知道,如果它適合你 – 2010-09-27 07:00:56

+0

你會在閱讀這個答案後更新你的答案嗎? http://stackoverflow.com/a/2926550/8418(我正試圖上傳到App Engine,然後修復..它的工作!) – Lipis 2012-06-07 01:43:56

0

我沒有教程回合它。在這裏你有一個例子:np。

POST/HTTP/1.1
主機:jmaster
的User-Agent:Mozilla的/ 5.0(視窗; U; Windows NT的5.1; PL; RV:1.9.2.10)的Gecko/20100914火狐/ 3.6.10
接受:text/html,application/xhtml + xml,application/xml; q = 0.9,/; q = 0.8
Accept-Language:pl,en-us; q = 0.7,en; q = 0.3
Accept-Encoding:gzip,deflate
Accept-Charset:ISO-8859-2,utf-8; q = 0.7,*; q = 0.7
引用者:http://shop/index.php/index/register/b/
Content-Type:multipart/form-data; boundary = --------------------------- 19187836022413
X-Forwarded-For:127.0.0.1
X-Forwarded-Host:jmaster
X - 轉發服務器:jmaster
連接:保持活動
的Content-Length:38682
------------------------- ---- 19187836022413
內容處理:表單數據; NAME = 「file2的」; filename =「Clipboard02.png」 Content-Type:image/png
‰PNG

...這是事實。
----------------------------- 19187836022413
您即將結束傳輸。
---------------------------- 19187836022413

希望這會有所幫助。