I am pretty new to both Android and WCF. I am making a user table with a very small image in the sql server db as varbinary. In my service I have the following OperationContract:
[OperationContract]
[WebGet(UriTemplate = "users/{emailAddress}/Image", ResponseFormat = WebMessageFormat.Json,
BodyStyle = WebMessageBodyStyle.Bare)]
System.IO.Stream GetImage(string emailAddress);
Implementation:
public System.IO.Stream GetImage(string emailAddress)
{
//parse personID, call DB
string query = "Select profilePic from Flows_Users WHERE emailAddress= @emailAddress";
SqlConnection objConnection = new SqlConnection();
DataSet ObjDataset = new DataSet();
SqlDataAdapter objAdapater = new SqlDataAdapter();
SqlCommand objCommand = new SqlCommand(query, objConnection);
objCommand.Parameters.Add("emailAddress", SqlDbType.NVarChar).Value = emailAddress;
objConnection.ConnectionString = ConfigurationManager.ConnectionStrings["ConnStr"].ToString();
objConnection.Open();
byte[] pic = (byte[])objCommand.ExecuteScalar();
objConnection.Close();
// if (pic == null)
// return null;
OutgoingWebResponseContext context = WebOperationContext.Current.OutgoingResponse;
// //if (image_not_found_in_DB)
// //{
// // context.StatusCode = System.Net.HttpStatusCode.Redirect;
// // context.Headers.Add(System.Net.HttpResponseHeader.Location, url_of_a_default_image);
// // return null;
// //}
//// everything is OK, so send image
context.Headers.Add(System.Net.HttpResponseHeader.CacheControl, "public");
context.ContentType = "image/jpeg";
// context.LastModified = date_image_was_stored_in_database;
context.StatusCode = System.Net.HttpStatusCode.OK;
return new System.IO.MemoryStream(pic);
}
Is there any way to read this into an Android Image View? I was unable to do it the base64 JSON way because the string kept terminating every time I tried to put it in a JSONArray.
Any help would be appreciated, I have been working on this since Saturday.
You are returning a MemoryStream object. Return a Base64 encoded byte array of your image instead.
On the Android side then you can turn it back into an image and place it in your ImageView.