public ItemsViewModel(PictureService _picService) { Title = "Fotografias"; picService = _picService; Items = new ObservableRangeCollection <PicItem>(); StItems = new List <ST>(); LoadItemsCommand = new Command(async() => await ExecuteLoadItemsCommand()); //RemoveItemCommand = new Command(async () => await ExecuteRemoveItemCommand()); MessagingCenter.Subscribe <NewItemPage, PicItem>(this, "AddItem", async(obj, item) => { var _item = item as PicItem; Items.Add(_item); Items.OrderByDescending(x => x.CreatedOn); await picService.AddItemAsync(_item); }); }
async Task <Image> TakePicture() { if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsPickPhotoSupported) { //DisplayAlert("No Camera", ":( No camera avaialble.", "OK"); } var fileName = System.DateTime.Now.Ticks.ToString(); var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Sample", Name = fileName }); if (file == null) { return(new Image()); } //DisplayAlert("File Location", file.Path, "OK"); Console.WriteLine(file.Path); var image = new Image { Aspect = Aspect.AspectFit }; image.Source = ImageSource.FromStream(() => { var stream = file.GetStream(); file.Dispose(); return(stream); }); var picService = new PictureService(); picService.AddItemAsync(new PicItem { FileName = fileName, Sent = false, CreatedOn = DateTime.Now }); return(image); }
public async Task <Image> TakePicture() { if (!CrossMedia.Current.IsCameraAvailable || !CrossMedia.Current.IsPickPhotoSupported) { //DisplayAlert("No Camera", ":( No camera avaialble.", "OK"); } UserDialogs.Instance.ShowLoading("Obteniendo posicion", MaskType.Black); var locator = CrossGeolocator.Current; locator.DesiredAccuracy = 25; var position = new Plugin.Geolocator.Abstractions.Position(); try { position = await locator.GetPositionAsync(timeoutMilliseconds : 10000); if (position == null) { } UserDialogs.Instance.HideLoading(); } catch (Exception ex) { UserDialogs.Instance.HideLoading(); } var fileName = System.DateTime.Now.Ticks.ToString(); var file = await CrossMedia.Current.TakePhotoAsync(new Plugin.Media.Abstractions.StoreCameraMediaOptions { Directory = "Sample", Name = fileName, PhotoSize = Plugin.Media.Abstractions.PhotoSize.Medium, CompressionQuality = 80 }); if (file == null) { return(new Image()); } //DisplayAlert("File Location", file.Path, "OK"); Console.WriteLine(file.Path); var image = new Image { Aspect = Aspect.AspectFit }; image.Source = ImageSource.FromStream(() => { var stream = file.GetStream(); file.Dispose(); return(stream); }); var stream_ = file.GetStream(); byte[] bytes = new byte[stream_.Length]; await stream_.ReadAsync(bytes, 0, (int)stream_.Length); var base64 = Convert.ToBase64String(bytes); Item = new PicItem { FileName = base64, Sent = false, CreatedOn = DateTime.UtcNow.ToLocalTime().ToString("s") + "Z", Latitude = position.Latitude, Longitude = position.Longitude, Color = "#FFFFFF" }; Item.ST_string = ""; Item.Folio_string = ""; await picService.AddItemAsync(Item); Items.Add(Item); //ESTO LO COMENTE RECIÉN MessagingCenter.Send(this, "AddItem", Item); //await Navigation.PopToRootAsync(); return(image); }