2012-06-08 41 views
2

我試圖使用Apache Commons HttpClient在Android設備和其他一些參數上發佈相機拍攝的圖像。無法使用HttpClient從Android設備相機發布圖像

我的帖子請求正常工作,服務器可以很好地讀取參數,但是我的圖片沒有上傳。上傳由PHP腳本處理,我可以確認正確的HTTP Post將導致圖像成功上傳,因爲我已經使用HTML表單測試了PHP腳本。我也可以在下面的代碼中確認這張照片!= null。

HttpClient httpClient = new DefaultHttpClient(); 
HttpPost postRequest = new HttpPost("http://myurl.com/upload.php"); 
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 

if (photo != null) { 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
    photo.compress(CompressFormat.JPEG, 75, bos); 
    byte[] data = bos.toByteArray(); 

    ByteArrayBody bab = new ByteArrayBody(data, "submission-1.jpg"); 

    reqEntity.addPart("file", bab); 
} 

reqEntity.addPart("date", new StringBody(date)); 
reqEntity.addPart("lat", new StringBody(latitude)); 
reqEntity.addPart("long", new StringBody(longitude)); 
reqEntity.addPart("name", new StringBody(name)); 
reqEntity.addPart("email", new StringBody(email)); 
reqEntity.addPart("comments", new StringBody(comments)); 

postRequest.setEntity(reqEntity); 
HttpResponse response = httpClient.execute(postRequest); 
+1

檢查byte []數據是否有東西? – Orlymee

+0

我剛剛證實,它確實包含數據 –

+0

@PeterWillsey:我面臨同樣的問題。但我沒有獲得MultipartEntity? –

回答

0

我想通了,希望別人會覺得這有幫助。我所要做的只是將文件類型添加到此行:

ByteArrayBody bab = new ByteArrayBody(data, "image/jpeg", "submission.jpg");