android - 从 Android 发送到 SQL 数据库后无法渲染图像

标签 android sql web-services ksoap

我有一个 Android 应用程序 (API 10),其功能之一是允许用户捕获照片并使用 ksoap 将其发送到 asp.net Web 服务。 Android 应用程序工作正常,将包括图像字节数组在内的所有数据发送到数据库。 SQL 数据库有一个用于存储字节数组的数据的 Image 字段。这一切都按预期进行。但是,当进行测试以确保没有任何内容损坏、图像保存正确等时,我尝试使用 .ashx 页面渲染图像,它只显示“损坏的图像”图标。我确信这是我错过的一些简单的东西,但是在盯着它看了很长时间之后,它没有意义。

下面是获取字节数组的 Android 应用程序 fragment :

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 应用程序的代码 fragment :

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 服务的 fragment :

[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();
    }
}

这个应用程序已经成功地使用了这个服务和另一个服务,唯一的问题是将图像文件作为字节数组提交,它可以正常工作,但我无法在测试中渲染图像以确保它发送正常。我确信我错过了一些简单的事情。感谢您的反馈...

最佳答案

我让它工作了,Android 代码工作正常,问题出在服务和我的测试网页上。该服务需要将传入的字节数组作为更新语句的 SQL 参数进行处理,如下所示:

cmd.Parameters.Add("@arrayToInsert", SqlDbType.Image, img.Length).Value = img;

其中“img”是来自 Android 的传入字节数组。然后在测试网页中,我像这样渲染图像:

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寻求建议!

关于android - 从 Android 发送到 SQL 数据库后无法渲染图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10179352/

相关文章:

android - java.lang.NoClassDefFoundError : org/apache/oltu/oauth2/client/request/OAuthClientRequest$TokenRequestBuilder

mysql - 在 MySQL 中获取基于月/年的所有行条目

Mysql SELECT 语句仅返回行,其中/如果给定数量的列不为空

web-services - 如何在 Tomcat Servlet 容器中验证和授权 webapp 请求

android - Android中有没有像MFC中的onShow一样的布局功能

android - 安全关闭数据库

php - 我将如何从 mysql 获取相同名称的值

web-services - 使用 Qooxdoo 从 Web 服务获取数据时遇到问题

java - 架构师迫切希望使用 SOAP over JMS

android - 颜色数组中 ListView 中每一行的随机颜色