2012-04-16 3 views
2

私は、ユーザーが写真をキャプチャしてksoapを使用してasp.net Webサービスに送信する機能の1つとして、Androidアプリケーション(API 10)を使用しています。 Androidアプリは正常に動作し、画像バイト配列を含むすべてのデータをデータベースに送信します。 SQLデータベースには、バイト配列が格納されているデータのImageフィールドがあります。これはすべて期待通りに機能します。しかし、何も壊れていないか、画像が正しく保存されているかなどをテストするときは、.ashxページを使用して画像をレンダリングしようとすると、「破損した画像」アイコンが表示されます。私はそれが私が行方不明のシンプルなものだと確信していますが、長いことそれを見つめた後、それは理にかなっていません。ここでは、AndroidからSQLデータベースに送信した後にイメージをレンダリングできません。

は、バイト配列をつかむAndroidアプリの抜粋です:

byte[] ba; 
String filepath = "/sdcard/"; 
File imagefile = new File(filepath + "img.jpg"); 
FileInputStream fis = new FileInputStream(imagefile); 
Bitmap bm = BitmapFactory.decodeStream(fis); 
Bitmap sbm = Bitmap.createScaledBitmap(bm, 640, 480, false); 
if(bm != null) 
{ 
    ByteArrayOutputStream out = new ByteArrayOutputStream(); 
    sbm.compress(Bitmap.CompressFormat.JPEG, 100, out); 
    ba = out.toByteArray(); 
} 

はここでSOAPを作成し、それを実行Androidアプリの抜粋です:

SoapObject request = new SoapObject(NAMESPACE, SEND_METHOD_NAME); 
request.addProperty("pkid", pkid); 
request.addProperty("img", ba); 
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11); 
envelope.dotNet = true; 
envelope.setOutputSoapObject(request); 
MarshalBase64 marshal = new MarshalBase64(); 
marshal.register(envelope); 
AndroidHttpTransport androidHttpTransport = new AndroidHttpTransport(SEND_URL); 
try 
{ 
    androidHttpTransport.call(SEND_SOAP_ACTION, envelope); 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 

そして、ここでは、スニペットですSOAPメッセージを受信するASP.Net Webサービスから:

[WebService(Namespace = "http://www.domain.com")] 
[System.Web.Services.Protocols.SoapDocumentService(RoutingStyle = System.Web.Services.Protocols.SoapServiceRoutingStyle.RequestElement)] 
[System.ComponentModel.ToolboxItem(false)] 
public class sendWorkOrderService : System.Web.Services.WebService 
{ 
    public SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["main"].ConnectionString); 

    [WebMethod(Description = "This is it", EnableSession = false)] 
    public void receive(int pkid, byte[] img) 
    { 
     if (img.Length > 0) 
     { 
      cmd = new SqlCommand("update table set photo = @arrayToInsert where pkid = " + pkid, con); 
      cmd.Parameters.Add("@arrayToInsert", SqlDbType.Image, 16).Value = img; 
     } 
     else 
     { 
      // do nothing 
     } 
     con.Open(); 
     cmd.ExecuteNonQuery(); 
     con.Close(); 
    } 
} 

このap pは既にこのサービスと別のサービスを正常に使用していますが、唯一の問題はイメージファイルをバイト配列として送信することですが、テストでイメージをレンダリングしてokを送信することはできません。私はそれが私が行方不明の単純なものだと確信しています。あなたのフィードバックに感謝します...

答えて

2

私はそれが働いて、Androidコードが正常に動作していた、問題はサービスと私のテストのWebページにあった。これと同様に、更新文のSQLパラメータとして入ってくるバイト配列を処理するために必要なサービス:

「IMG」はアンドロイドからの着信バイト配列である
cmd.Parameters.Add("@arrayToInsert", SqlDbType.Image, img.Length).Value = img; 

。テストウェブページでは、次のように画像をレンダリングしました。

MemoryStream stream = new MemoryStream(); 
SqlCommand command = new SqlCommand("select photo from table where pkid = @ImageID", con); 
command.Parameters.AddWithValue("@ImageID", Request.QueryString["ImageID"]); 
byte[] image = (byte[])command.ExecuteScalar(); 
stream.Write(image, 0, image.Length); 
System.Drawing.Image bitmap = new Bitmap(stream); 
Response.ContentType = "image/jpeg"; 
bitmap.Save(Response.OutputStream, ImageFormat.Jpeg); 
con.Close(); 
stream.Close(); 

これですべてのことが必要です。アドバイスのために@andrewr73に感謝します!

関連する問題