2012-03-28 177 views
0

我即將寫服務器端應用程序(最有可能它會是PHP,但也可能是JAVA)和Android客戶端應用程序。我試圖找出從android應用發送照片到服務器並在服務器端接收照片的最佳方式。如果這種方式來優化/序列化一次發送多個圖片?
請給我一些參考或提示。
在此先感謝。從Android應用程序發送照片到服務器端

回答

1

你可以使用HTTP發佈這個。 GET ByteArrayOutputStream和壓縮的JPEG圖像,並使用ByteArrayBody和使用的HttpClient

 ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

     bm.compress(CompressFormat.JPEG, 75, bos); 

     byte[] data = bos.toByteArray(); 

     HttpClient httpClient = new DefaultHttpClient(); 

     HttpPost postRequest = new HttpPost(

       "http://10.0.2.2/cfc/iphoneWebservice.cfc?returnformat=json&method=testUpload"); 

     ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg"); 

     // File file= new File("/mnt/sdcard/forest.png"); 

     // FileBody bin = new FileBody(file); 

     MultipartEntity reqEntity = new MultipartEntity(

       HttpMultipartMode.BROWSER_COMPATIBLE); 

     reqEntity.addPart("uploaded", bab); 

     reqEntity.addPart("photoCaption", new StringBody("sfsdfsdf")); 

     postRequest.setEntity(reqEntity); 

     HttpResponse response = httpClient.execute(postRequest); 

     BufferedReader reader = new BufferedReader(new InputStreamReader(

       response.getEntity().getContent(), "UTF-8")); 

     String sResponse; 

     StringBuilder s = new StringBuilder(); 



     while ((sResponse = reader.readLine()) != null) { 

      s = s.append(sResponse); 

     } 

你可以在這裏找到相關的代碼張貼。 http://vikaskanani.wordpress.com/2011/01/11/android-upload-image-or-file-using-http-post-multi-part/

+0

呃,謝謝!我真的不指望現成的代碼:)但再次感謝你 – radek 2012-03-28 23:26:32

相關問題