无法查看从Android上传的图片

时间:2015-03-17 12:40:26

标签: asp.net android-image android-ksoap2 ashx

我使用以下代码从Android手机上传图片。

 b1.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                String NAMESPACE = "http://tempuri.org/";
                String URL = "http://mywebservice.asmx";
                String SOAP_ACTION = "http://tempuri.org/";

                try{
                    SoapObject so=new SoapObject(NAMESPACE, "insertPicture");
                    so.addProperty("binPicture", array);
                    SoapSerializationEnvelope sse=new SoapSerializationEnvelope(SoapEnvelope.VER11);
                    sse.dotNet=true;
                    sse.setOutputSoapObject(so);
                    Toast.makeText(getApplicationContext(), new uploadPic().execute(sse).get(), Toast.LENGTH_LONG).show();
                } catch (Exception e) {
                    Toast.makeText(getApplicationContext(),e.getMessage() + " Error",Toast.LENGTH_LONG).show();
                }
            }
        });
    }
    public static byte[] streamToBytes(InputStream is) {
        ByteArrayOutputStream os = new ByteArrayOutputStream(1024);
        byte[] buffer = new byte[1024];
        int len;
        try {
            while ((len = is.read(buffer)) >= 0) {
                os.write(buffer, 0, len);
            }
        } catch (java.io.IOException e) {
        Log.e("TAG", e.getMessage());
        }
        return os.toByteArray();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    private class uploadPic extends AsyncTask<SoapSerializationEnvelope, Void, String> {
        protected String doInBackground(SoapSerializationEnvelope... params) {

        String URL = "<My Web Service>";
        String SOAP_ACTION = "http://tempuri.org/";

        HttpTransportSE htse = new HttpTransportSE(URL);

        new MarshalBase64().register(params[0]);
        String responseMsg = "";
        try {
            htse.call(SOAP_ACTION + "insertPicture", params[0]);
            SoapPrimitive response=(SoapPrimitive) params[0].getResponse();
            responseMsg = response.toString();
        }
        catch (Exception e) {
            responseMsg = e.getMessage();
        }
        return responseMsg;

    }
}

当我调用以下Web方法时,图像将上传到sql数据库

 <WebMethod> _
Public Function insertPicture(ByVal binPicture As String) As String
    Dim sqlStatement As String
    Dim connectionString As String = System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString()
    Try
        sqlStatement = "Insert INTO PICTURE (PICTURE) VALUES (CONVERT(VARBINARY(MAX),'" & binPicture & "'))"

        Using connection As New SqlConnection(connectionString)

            Dim command As New SqlCommand(sqlStatement, connection)
            connection.Open()
            command.ExecuteNonQuery()
        End Using
        Return "Success"
    Catch ex As Exception
        Return ex.Message
    End Try

End Function

我正在使用处理程序来查看aspx页面上的图像

Imports System.Web
Imports System.Web.Services
Imports System

Imports System.Data
Imports System.Data.SqlClient


Public Class NWEmpPhotoHandler
    Implements System.Web.IHttpHandler
    Public Sub ProcessRequest(context As HttpContext) Implements IHttpHandler.ProcessRequest

        Dim id As String = context.Request.QueryString("id")

        Dim con As SqlConnection = New SqlConnection(System.Configuration.ConfigurationManager.AppSettings("ConnectionString").ToString())
        Dim cmd As SqlCommand = New SqlCommand("select picture from picture where id = @empid", con)
        cmd.CommandType = CommandType.Text
        cmd.Parameters.AddWithValue("@empid", id)

        con.Open()

        Dim dReader As SqlDataReader = cmd.ExecuteReader()
        dReader.Read()
        context.Response.BinaryWrite(DirectCast(dReader("picture"), Byte()))
        dReader.Close()
        con.Close()


    End Sub

    ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
        Get
            Return False
        End Get
    End Property

End Class

当我尝试在HTML页面中访问此图像时,图像不会显示

<img src="EmpPhoto.ashx?id=2008" />

我哪里出错

a)上传图片 b)保存图像 c)检索图像

???

请帮帮我。

感谢。

0 个答案:

没有答案