public static async Task<string> SendProfilePicAndStatusNote(string note, MediaItem profileImgae, string imageType)
		{
			try {
				if (!CrossConnectivity.Current.IsConnected) {
					return "404";
				}
				User user = App.Settings.GetUser();

				var client = new HttpClient();

				client.DefaultRequestHeaders.Add("Post", "application/json");
				client.BaseAddress = new Uri(Constants.SERVICE_BASE_URL);
				var url = "api.php?action=profileimageandstatus";
				MultipartFormDataContent content = new MultipartFormDataContent();

				content.Add(new StringContent(user.UserId.ToString(), Encoding.UTF8), "user_id");
				//
				//				if(!string.IsNullOrEmpty(profileImgae))
				//				{
				//					content.Add(new StringContent("media info string ", Encoding.UTF8), "profileimg");
				//					if (!string.IsNullOrEmpty(imageType)) {
				//						content.Add(new StringContent(imageType, Encoding.UTF8), "filetype");
				//					}
				//				}

				if (!string.IsNullOrEmpty(note)) {
					content.Add(new StringContent(note, Encoding.UTF8), "note");
				}

				if(profileImgae != null)
				{
					content.Add(new StringContent(profileImgae.MediaString, Encoding.UTF8), "profileimg");
					content.Add(new StringContent(imageType, Encoding.UTF8), "filetype");
				}

				var response = await client.PostAsync(url, content);
				if (response != null )
				{
					var responseJson = response.Content.ReadAsStringAsync().Result;
					var rootobject = JsonConvert.DeserializeObject<RegTokenResponse>(responseJson);
					if (rootobject != null && rootobject.code != null)
					{
						if (!string.IsNullOrEmpty(imageType)) {
							return rootobject.text;// return the url of uploaded picture.
						}
						else{
							return rootobject.code;
						}
					}
					else
					{
						return "404";
					}
				}
				else
				{
					return "404";
				}

			} catch (Exception ex) {

			}
			return "404";
		}
        public static void ReceiveVideoFromWindows(MemoryStream ms, string path)
        {
            //AddFileToMediaArray(ms, fileName, PurposeColor.Constants.MediaType.Video);

            try
            {
                MediaPost mediaWeb = new MediaPost();
                mediaWeb.event_details = string.IsNullOrWhiteSpace(eventDescription.Text) ? string.Empty : eventDescription.Text;
                mediaWeb.event_title = string.IsNullOrWhiteSpace(eventTitle.Text) ? string.Empty : eventTitle.Text;
				User user = App.Settings.GetUser();
				mediaWeb.user_id = Convert.ToInt32(user.UserId);

                string imgType = System.IO.Path.GetExtension(path);
                string fileName = System.IO.Path.GetFileName(path);

                App.PreviewListSource.Add(new PreviewItem { Name = fileName, Path = path, Image = Device.OnPlatform("video.png", "video.png", "//Assets//video.png") });

                imgType = imgType.Replace(".", "");
                
                
                Byte[] inArray = ms.ToArray();
                Char[] outArray = new Char[(int)(ms.ToArray().Length * 1.34)];
                Convert.ToBase64CharArray(inArray, 0, inArray.Length, outArray, 0);
                string test2 = new string(outArray);
                App.ExtentionArray.Add(imgType);
                MediaItem item = new MediaItem();
                item.MediaString = test2;
                item.Name = fileName;
                App.MediaArray.Add(item);

                inArray = null;
                outArray = null;
                test2 = null;
                item = null;
                imgType = string.Empty;
                fileName = string.Empty;

                StackLayout preview = (StackLayout)masterLayout.Children.FirstOrDefault(pick => pick.ClassId == "preview");
                masterLayout.Children.Remove(preview);
                preview = null;
                previewListView.ItemsSource = null;
                previewListView.ItemsSource = App.PreviewListSource;
                masterLayout.AddChildToLayout(listContainer, 5, 60);

                GC.Collect();
            }
            catch (Exception ex)
            {
                var test = ex.Message;
            }
        }
		async public void AddFileToMediaArray(MemoryStream ms, string path, PurposeColor.Constants.MediaType mediaType)
		{
			try
			{
				MediaPost mediaWeb = new MediaPost();

				string imgType = System.IO.Path.GetExtension(path);
				string fileName = System.IO.Path.GetFileName(path);
				MediaItem mediaItem = new MediaItem();

				#region MEDIA COMPRESSION AND SERIALISING

				if (ms != null)
				{
					imgType = imgType.Replace(".", "");
					if (mediaType == Constants.MediaType.Image)
					{
						MemoryStream compressedStream = new MemoryStream();
						IResize resize = DependencyService.Get<IResize>();
						Byte[] resizedOutput = resize.Resize(ms.ToArray(), (float)(App.screenWidth * App.screenDensity), (float)(App.screenHeight * App.screenDensity), path);
						if(resizedOutput== null)
							return;

						MemoryStream resizedStream = new MemoryStream(resizedOutput);
						compressedStream = resize.CompessImage(25, resizedStream);


						var path2 = System.IO.Path.Combine(App.DownloadsPath, fileName);

						Byte[] inArray = compressedStream.ToArray();
						Char[] outArray = new Char[(int)(compressedStream.ToArray().Length * 1.34)];
						Convert.ToBase64CharArray(inArray, 0, inArray.Length, outArray, 0);
						string test2 = new string(outArray);
						App.ExtentionArray.Add(imgType);

						mediaItem.MediaString = test2;
						mediaItem.Name = fileName;
						App.MediaArray.Add(mediaItem);

						inArray = null;
						outArray = null;
						test2 = null;

						resizedOutput = null;
						GC.Collect();
					}
					//
					//					imgType = string.Empty;
					//					fileName = string.Empty;

				}

				#endregion

				if(mediaItem != null)
				{
					string serviceResult = await PurposeColor.Service.ServiceHelper.SendProfilePicAndStatusNote(string.Empty, mediaItem, imgType);
					if (serviceResult != null  ) 
					{
						if(callerObject != null)
						{
							callerObject.profilePic.Source = Constants.SERVICE_BASE_URL + serviceResult;
						}

						User user = App.Settings.GetUser();
						if(user != null)
						{
							user.ProfileImageUrl = serviceResult;
							await App.Settings.SaveUser(user);
						}
					}
				}

				mediaItem = null;
				if (callerObject != null) {
					callerObject.progressBar.HideProgressbar();
				}
			}
			catch (Exception ex)
			{
				var test = ex.Message;
			}
		}
        public void AddFileToMediaArray(MemoryStream ms, string path, PurposeColor.Constants.MediaType mediaType)
        {
            try
            {
                MediaPost mediaWeb = new MediaPost();
                mediaWeb.event_details = string.IsNullOrWhiteSpace(eventDescription.Text) ? string.Empty : eventDescription.Text;
                mediaWeb.event_title = string.IsNullOrWhiteSpace(eventTitle.Text) ? string.Empty : eventTitle.Text;
				User user = App.Settings.GetUser();
                
				mediaWeb.user_id = Convert.ToInt32(user.UserId);

                string imgType = System.IO.Path.GetExtension(path);
                string fileName = System.IO.Path.GetFileName(path);

                if (mediaType == Constants.MediaType.Image)
                {
                    App.PreviewListSource.Add(new PreviewItem { Path = path, Name = fileName, Image = Device.OnPlatform("image.png", "image.png", "//Assets//image.png") });//Device.OnPlatform("delete_button.png", "delete_button.png", "//Assets//delete_button.png");
                }
                else if (mediaType == Constants.MediaType.Video)
                {
					/*string videoThumpName = Path.GetFileNameWithoutExtension( path ) + ".jpg";
					string downloadFilePath = Path.Combine(App.DownloadsPath, videoThumpName);

					App.PreviewListSource.Add(new PreviewItem { Path = path, Name = fileName, Image = Device.OnPlatform( downloadFilePath, downloadFilePath, "//Assets//video.png") });*/

                    App.PreviewListSource.Add(new PreviewItem { Path = path, Name = fileName, Image = Device.OnPlatform("video.png", "video.png", "//Assets//video.png") });
                }
                else
                {
                    App.PreviewListSource.Add(new PreviewItem { Path = path, Name = fileName, Image = Device.OnPlatform("mic.png", "mic.png", "//Assets//mic.png") });
                }

                #region MEDIA COMPRESSION AND SERIALISING

                if (ms != null)
                {
                    imgType = imgType.Replace(".", "");
                    if (mediaType == Constants.MediaType.Image)
                    {
                        MemoryStream compressedStream = new MemoryStream();
                        IResize resize = DependencyService.Get<IResize>();
						Byte[] resizedOutput = resize.Resize(ms.ToArray(), (float)(App.screenWidth * App.screenDensity), (float)(App.screenHeight *0.50* App.screenDensity), path);
                        MemoryStream resizedStream = new MemoryStream(resizedOutput);
						int streamLength = (int)resizedStream.Length;

						int compressionRate  = 100;

						#region compression ratio
						if (streamLength < 20000) {
							compressionRate = 100;
						}
						else if (streamLength < 40000) {
							compressionRate = App.screenDensity > 2 ? 100: 95;
						}
						else if (streamLength < 50000) {
							compressionRate = App.screenDensity > 2 ? 100: 91;
						}
						else if (streamLength < 100000) {
							compressionRate = App.screenDensity > 2 ? 100: 90;
						}
						else if (streamLength < 200000) {
							compressionRate = App.screenDensity > 2 ? 99: 89;
						}
						else if (streamLength <300000) {
							compressionRate = App.screenDensity > 2 ? 98: 88;
						}
						else if (streamLength < 400000) {
							compressionRate = App.screenDensity > 2 ? 97: 87;
						}
						else if (streamLength < 500000) {
							compressionRate = App.screenDensity > 2 ? 96: 86;
						}
						else if (streamLength < 600000) {
							compressionRate = App.screenDensity > 2 ? 95: 86;
						}
						else if (streamLength < 700000) {
							compressionRate = App.screenDensity > 2 ? 94: 86;
						}
						else if (streamLength < 900000) {
							compressionRate = App.screenDensity > 2 ? 93: 86;
						}
						else if (streamLength < 1000000) {
							compressionRate = App.screenDensity > 2 ? 92: 86;
						}
						else if (streamLength < 2000000) {
							compressionRate = App.screenDensity > 2 ? 91: 86;
						}
						else {
							compressionRate = App.screenDensity > 2 ? 90: 85;
						}
						#endregion


						compressedStream = resize.CompessImage(compressionRate, resizedStream);

						Byte[] inArray = resizedStream.ToArray();
						Char[] outArray = new Char[(int)(resizedStream.ToArray().Length * 1.34)];
                        Convert.ToBase64CharArray(inArray, 0, inArray.Length, outArray, 0);
                        string test2 = new string(outArray);
                        App.ExtentionArray.Add(imgType);
                        MediaItem item = new MediaItem();
                        item.MediaString = test2;
                        item.Name = fileName;
                        App.MediaArray.Add(item);

                        inArray = null;
                        outArray = null;
                        test2 = null;
                        item = null;
                        resizedOutput = null;
                        GC.Collect();
                    }
                    else
                    {
                        Byte[] inArray = ms.ToArray();
                        Char[] outArray = new Char[(int)(ms.ToArray().Length * 1.34)];
                        Convert.ToBase64CharArray(inArray, 0, inArray.Length, outArray, 0);
                        string test2 = new string(outArray);
                        App.ExtentionArray.Add(imgType);
                        MediaItem item = new MediaItem();
                        item.MediaString = test2;
                        item.Name = fileName;
						if( mediaType == Constants.MediaType.Video )
						{
							IVideoCompressor compressor = DependencyService.Get<IVideoCompressor>();
							string imgThumbPath =  App.DownloadsPath +  Path.GetFileNameWithoutExtension( path ) + ".jpg";
							MemoryStream thumbStream = compressor.CreateVideoThumbnail( path, imgThumbPath );

							Byte[] thumbInArray = thumbStream.ToArray();
							Char[] thumbOutArray = new Char[(int)(thumbStream.ToArray().Length * 1.34)];
							Convert.ToBase64CharArray(thumbInArray, 0, thumbInArray.Length, thumbOutArray, 0);
							string thumbString = new string(thumbOutArray);

							item.MediaType = Constants.MediaType.Video;
							item.MediaThumbString = thumbString;
							ms.Dispose();
							thumbStream.Dispose();

							thumbInArray = null;
							thumbOutArray = null;
						}
                        App.MediaArray.Add(item);

                        inArray = null;
                        outArray = null;
                        test2 = null;
                        item = null;
                        GC.Collect();
                    }
                    imgType = string.Empty;
                    fileName = string.Empty;

                }
                
                #endregion

                StackLayout preview = (StackLayout)masterLayout.Children.FirstOrDefault(pick => pick.ClassId == "preview");
                if (preview != null)
                {
                    masterLayout.Children.Remove(preview);
                    preview = null;
                    previewListView.ItemsSource = null;

                    previewListView.ItemsSource = App.PreviewListSource;
                    masterLayout.AddChildToLayout(listContainer, 5, 60);
                }
                
            }
            catch (Exception ex)
            {
                DisplayAlert(Constants.ALERT_TITLE, "Unable to add the media", Constants.ALERT_OK);
                var test = ex.Message;
            }
        }