protected override Android.Graphics.Bitmap RunInBackground(params Profile[] @params) { String imageUrl = @params[0].imageUri; String userId = @params[0].ID; Bitmap image = null; bool AvatarIsDefault = @params[0].AvatarIsDefault; if (imageUrl == null) { return(null); } try { byte[] data = OperationWithBitmap.Retrieve(context, userId); if (data == null) { image = OperationWithBitmap.GetFromUrl(imageUrl); var bitmapData = OperationWithBitmap.ConvertToByteArray(image); OperationWithBitmap.Cache(context, bitmapData, userId); } else { image = OperationWithBitmap.ConvertFromByteArray(data); } } catch (Exception e) { Console.Out.WriteLine(e.Message); } return(image); }
protected override void OnPostExecute(Task <Bitmap> result) { byte[] bitmapData = OperationWithBitmap.ConvertToByteArray(result.Result); db.UpdateAvatarUrl(New: url, at: UserId); OperationWithBitmap.Cache(Application.Context, bitmapData, UserId); base.OnPostExecute(result); }
public static async Task <String> UploadImage(Bitmap image) { string boundary = "----------------------------" + DateTime.Now.Ticks.ToString("x"); HttpWebRequest request = (HttpWebRequest)WebRequest.Create(await GetUploadUrl()); request.ContentType = "multipart/form-data; boundary=" + boundary; request.Method = "POST"; request.KeepAlive = true; request.Headers.Add("Access-Token", CurrentAccessToken); Stream memStream = new System.IO.MemoryStream(); var boundarybytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "\r\n"); var endBoundaryBytes = System.Text.Encoding.ASCII.GetBytes("\r\n--" + boundary + "--"); string formdataTemplate = "\r\n--" + boundary + "\r\nContent-Disposition: form-data; name=\"{0}\";\r\n\r\n{1}"; string headerTemplate = "Content-Disposition: form-data; name=\"{0}\"; filename=\"{1}\"\r\n" + "Content-Type: application/octet-stream\r\n\r\n"; byte[] file = OperationWithBitmap.ConvertToByteArray(image); memStream.Write(boundarybytes, 0, boundarybytes.Length); var header = string.Format(headerTemplate, "uplTheFile", file); var headerbytes = System.Text.Encoding.UTF8.GetBytes(header); memStream.Write(headerbytes, 0, headerbytes.Length); memStream.Write(file, 0, file.Length); memStream.Write(endBoundaryBytes, 0, endBoundaryBytes.Length); request.ContentLength = memStream.Length; using (Stream requestStream = request.GetRequestStream()) { memStream.Position = 0; byte[] tempBuffer = new byte[memStream.Length]; memStream.Read(tempBuffer, 0, tempBuffer.Length); memStream.Close(); requestStream.Write(tempBuffer, 0, tempBuffer.Length); } JsonValue jsonDoc; using (var response = request.GetResponse()) { using (Stream stream = response.GetResponseStream()) { jsonDoc = await Task.Run(() => JsonValue.Load(stream)); } } return(jsonDoc["imageUrl"]); }
protected override Java.Lang.Void RunInBackground(params object[] @params) { if (@params[0].GetType() == typeof(HistoryListItem)) { HistoryListItem historyItem = @params[0] as HistoryListItem; UsersDatabase db = new UsersDatabase(); UserListItem user = db.GetUserByID(historyItem.fromUserID); try { byte[] data = OperationWithBitmap.Retrieve(context, user.ID); if (data == null) { Bitmap curImage = OperationWithBitmap.GetFromUrl(user.url); //user.AvatarIsDefault = OperationWithBitmap.isDefault(curImage); //if (!user.AvatarIsDefault) //{ historyItem.image = curImage; //} var bitmapData = OperationWithBitmap.ConvertToByteArray(historyItem.image); OperationWithBitmap.Cache(context, bitmapData, historyItem.ID); } else { //if (!user.AvatarIsDefault) historyItem.image = OperationWithBitmap.ConvertFromByteArray(data); } } catch (Exception e) { Console.Out.WriteLine(e.Message); } } else if (@params[0].GetType() == typeof(UserListItem)) { UserListItem user = @params[0] as UserListItem; bool AvatarIsDefault = user.AvatarIsDefault; try { byte[] data = OperationWithBitmap.Retrieve(context, user.ID); if (data == null) { Bitmap curImage = OperationWithBitmap.GetFromUrl(user.url); //user.AvatarIsDefault = OperationWithBitmap.isDefault(curImage); //if (!user.AvatarIsDefault) //{ user.image = curImage; // UsersDatabase db = new UsersDatabase(); // db.UpdateAvatarState(user); //} var bitmapData = OperationWithBitmap.ConvertToByteArray(user.image); OperationWithBitmap.Cache(context, bitmapData, user.ID); } else { //if (!user.AvatarIsDefault) user.image = OperationWithBitmap.ConvertFromByteArray(data); } } catch (Exception e) { Console.Out.WriteLine(e.Message); } } else { throw new Exception("Wrong ItemList type."); } return(null); }