2012-03-28 173 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

相关问题