2011-10-03 43 views

回答

19

檢查這個代碼與標題,標題,名稱等發送圖像,

HttpClient httpClient = new DefaultHttpClient(); 
    HttpPost postRequest = new HttpPost("You Link"); 
    MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE); 
    reqEntity.addPart("name", new StringBody("Name")); 
    reqEntity.addPart("Id", new StringBody("ID")); 
    reqEntity.addPart("title",new StringBody("TITLE")); 
    reqEntity.addPart("caption", new StringBody("Caption")); 
    try{ 
     ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
     bitmap.compress(CompressFormat.JPEG, 75, bos); 
     byte[] data = bos.toByteArray(); 
     ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg"); 
     reqEntity.addPart("picture", bab); 
    } 
    catch(Exception e){ 
     //Log.v("Exception in Image", ""+e); 
     reqEntity.addPart("picture", new StringBody("")); 
    } 
    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); 
    } 

其中位圖圖像位圖。

如果您發現任何問題,請告訴我。

謝謝Venky。

+0

太棒了。視頻上傳怎麼樣? –

+0

+1謝謝你。你拯救了我的一天。再次感謝。 – Sajmon

+0

但是這不是在Android Studio @Venky上發生 –