[英]Upload image from Android to WCF C # /. NET
早上好,我需要將存儲卡中的圖像從本機發送到我在wcf / C#中執行的Web服務中,以至於不知道C#是公司的必要條件,我什至可以發送適用於WCF的Strem,但無法轉換和轉換為Bitmap或其他內容。
按照我的android代碼在wcf中制作帖子圖像:
/**
*
* Método responsável por enviar imagem para o servidor
*
* @param String caminho
* @author Douglas Costa <douglas.cst90@gmail.com.br>
* @since 01/07/2013 18:27:49
* @version 1.0
*/
public static void upload(String caminho){
DefaultHttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://192.168.0.205:8070/Service/uploadImagem");
ResponseHandler<String> responseHandler = new BasicResponseHandler();
File file = new File(caminho);
//This is the new shit to deal with MIME
MultipartEntity entity = new MultipartEntity();
entity.addPart("image", new FileBody(file, "image/jpeg"));
httppost.setEntity(entity);
try {
String responseString = httpclient.execute(httppost, responseHandler);
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
這是我關於C#中WCF方法的帖子,該帖子從圖像接收流:
/// Método POST que recebe um Stream do Android
/// </summary>
/// <param name="imagem"></param>
/// <returns></returns>
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "uploadImagem")]
public Bitmap uploadImagem(Stream imagem)
{
try
{
byte[] buffer = new byte[16 * 1024];
using (MemoryStream ms = new MemoryStream())
{
int read;
while ((read = imagem.Read(buffer, 0, buffer.Length)) > 0)
{
ms.Write(buffer, 0, read);
}
buffer = ms.ToArray();
}
using (MemoryStream mStream = new MemoryStream())
{
mStream.Write(buffer, 0, buffer.Length);
Bitmap bm = new Bitmap(mStream);
return bm;
}
}
catch (Exception)
{
return null;
}
}
已經嘗試了幾種轉換流的方法,也不知道我發送的方法是否正確,但是我有一個類似的方法可以在JAVA Web服務中使用。
感謝有人可以幫助我使用C#。
對不起,英語錯誤,謝謝。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.