public async Task LoadMediaAssets() { try { if (source.IsCancellationRequested) { source = new CancellationTokenSource(); token = source.Token; } else { token = source.Token; } MediaAssets.Clear(); //clear list if already exists /*/ * Create default camera image as the first one * so when click this image we can call camera action later */ MediaAssest defaultmedia = new MediaAssest(); defaultmedia.PreviewPath = "group.png"; defaultmedia.IsSelectable = false; MediaAssets.Add(defaultmedia); await _mediaService.RetrieveMediaAssetsAsync(token); } catch (TaskCanceledException) { Console.WriteLine("Task was cancelled"); } }
void imageSkipTapped(System.Object sender, System.EventArgs e) { if (Preferences.Get("ProfileImage", null) == null || Preferences.Get("ProfileImage", null) == "default5.png") { //if first time "ProfileImage" is null or profile not choosen bodyContent.Opacity = 1; bodyContent.InputTransparent = false; imageselector.IsVisible = false; isSelected = false; selectedMediaAsset = null; if (selectedFrame != null) { selectedFrame.BackgroundColor = Color.Transparent; } Preferences.Set("ProfileImage", "default"); //skip pressed so store the data in Preferences with key "ProfileImage" and value as "default" ImageSkipOrSelectImageClickEvent(); //function handles if image choosen display the selected image or else display default image } else { // a valid profile image is already selected so this is second time or any number of time // so no need to set the preferences bodyContent.Opacity = 1; bodyContent.InputTransparent = false; imageselector.IsVisible = false; isSelected = false; selectedMediaAsset = null; if (selectedFrame != null) { selectedFrame.BackgroundColor = Color.Transparent; } ImageSkipOrSelectImageClickEvent(); } }
async void imageNextTapped(System.Object sender, System.EventArgs e) { bodyContent.Opacity = 1; bodyContent.InputTransparent = false; imageselector.IsVisible = false; string path = await Xamarin.Forms.DependencyService.Get <IMediaService>().StoreProfileImage(selectedMediaAsset.Path); //store the image in app folder Preferences.Set("ProfileImage", path); //set the path of the image in preferences ImageSkipOrSelectImageClickEvent(); isSelected = false; selectedMediaAsset = null; selectedFrame.BackgroundColor = Color.Transparent; }
async Task <IList <MediaAssest> > LoadMediaAsync() { IList <MediaAssest> assets = new List <MediaAssest>(); var imageManager = new PHCachingImageManager(); var hasPermission = await RequestPermissionAsync(); if (hasPermission) { await Task.Run(async() => { var thumbnailRequestOptions = new PHImageRequestOptions(); thumbnailRequestOptions.ResizeMode = PHImageRequestOptionsResizeMode.Fast; thumbnailRequestOptions.DeliveryMode = PHImageRequestOptionsDeliveryMode.FastFormat; thumbnailRequestOptions.NetworkAccessAllowed = true; thumbnailRequestOptions.Synchronous = true; var requestOptions = new PHImageRequestOptions(); requestOptions.ResizeMode = PHImageRequestOptionsResizeMode.Exact; requestOptions.DeliveryMode = PHImageRequestOptionsDeliveryMode.HighQualityFormat; requestOptions.NetworkAccessAllowed = true; requestOptions.Synchronous = true; var fetchOptions = new PHFetchOptions(); fetchOptions.SortDescriptors = new NSSortDescriptor[] { new NSSortDescriptor("creationDate", false) }; fetchOptions.Predicate = NSPredicate.FromFormat($"mediaType == {(int)PHAssetMediaType.Image}"); //fetchOptions.Predicate = NSPredicate.FromFormat($"mediaType == {(int)PHAssetMediaType.Image} || mediaType == {(int)PHAssetMediaType.Video}"); var fetchResults = PHAsset.FetchAssets(fetchOptions); var tmpPath = Path.GetTempPath(); var allAssets = fetchResults.Select(p => p as PHAsset).ToArray(); var thumbnailSize = new CGSize(1000.0f, 1000.0f); imageManager.StartCaching(allAssets, thumbnailSize, PHImageContentMode.AspectFit, thumbnailRequestOptions); //imageManager.StartCaching(allAssets, PHImageManager.MaximumSize, PHImageContentMode.AspectFit, requestOptions); foreach (var result in fetchResults) { if (CancelToken.Value.IsCancellationRequested) { break; } var phAsset = (result as PHAsset); var name = PHAssetResource.GetAssetResources(phAsset)?.FirstOrDefault()?.OriginalFilename; var asset = new MediaAssest() { Id = phAsset.LocalIdentifier, Name = name, Type = phAsset.MediaType == PHAssetMediaType.Image ? MediaAssetType.Image : MediaAssetType.Video, IsSelectable = true, }; imageManager.RequestImageForAsset(phAsset, thumbnailSize, PHImageContentMode.Default, thumbnailRequestOptions, (image, info) => { if (image != null) { NSData imageData = null; if (image.CGImage.RenderingIntent == CGColorRenderingIntent.Default) { imageData = image.AsJPEG(0.8f); } else { imageData = image.AsPNG(); } if (imageData != null) { var fileName = Path.Combine(tmpPath, $"tmp_thumbnail_{Path.GetFileNameWithoutExtension(name)}.jpg"); NSError error = null; imageData.Save(fileName, true, out error); if (error == null) { asset.PreviewPath = fileName; } } } }); switch (phAsset.MediaType) { case PHAssetMediaType.Image: imageManager.RequestImageForAsset(phAsset, PHImageManager.MaximumSize, PHImageContentMode.AspectFit, requestOptions, (image, info) => { if (image != null) { NSData imageData = null; if (image.CGImage.RenderingIntent == CGColorRenderingIntent.Default) { imageData = image.AsJPEG(0.8f); } else { imageData = image.AsPNG(); } if (imageData != null) { var fileName = Path.Combine(tmpPath, $"tmp_{name}"); NSError error = null; imageData.Save(fileName, true, out error); if (error == null) { asset.Path = fileName; } } } }); break; case PHAssetMediaType.Video: var videoRequestOptions = new PHVideoRequestOptions(); videoRequestOptions.NetworkAccessAllowed = true; var tcs = new TaskCompletionSource <bool>(); imageManager.RequestAvAsset(phAsset, null, (vAsset, audioMix, info) => { var avAsset = vAsset as AVUrlAsset; var avData = NSData.FromUrl(avAsset.Url); NSError error = null; var path = Path.Combine(tmpPath, $"tmp_{name}"); avData.Save(path, true, out error); if (error == null) { asset.Path = path; tcs.TrySetResult(true); } else { tcs.TrySetResult(false); } }); await tcs.Task; break; } if (CancelToken.Value.IsCancellationRequested) { break; } else { UIApplication.SharedApplication.InvokeOnMainThread(delegate { OnMediaAssetLoaded?.Invoke(this, new MediaEventArgs(asset)); }); } assets.Add(asset); if (requestStop) { break; } } }); imageManager.StopCaching(); } return(assets); }
async Task <IList <MediaAssest> > LoadMediaAsync() { IList <MediaAssest> assets = new List <MediaAssest>(); var hasPermission = await RequestPermissionAsync(); if (hasPermission) { var uri = MediaStore.Files.GetContentUri("external"); var ctx = Android.App.Application.Context; await Task.Run(async() => { var cursor = ctx.ApplicationContext.ContentResolver.Query(uri, new string[] { MediaStore.Files.FileColumns.Id, MediaStore.Files.FileColumns.Data, MediaStore.Files.FileColumns.DateAdded, MediaStore.Files.FileColumns.MediaType, MediaStore.Files.FileColumns.MimeType, MediaStore.Files.FileColumns.Title, MediaStore.Files.FileColumns.Parent, MediaStore.Files.FileColumns.DisplayName, MediaStore.Files.FileColumns.Size //}, $"{MediaStore.Files.FileColumns.MediaType} = {(int)MediaType.Image} OR {MediaStore.Files.FileColumns.MediaType} = {(int)MediaType.Video}", null, $"{MediaStore.Files.FileColumns.DateAdded} DESC"); }, $"{MediaStore.Files.FileColumns.MediaType} = {(int)MediaType.Image}", null, $"{MediaStore.Files.FileColumns.DateAdded} DESC"); if (cursor.Count > 0) { while (cursor.MoveToNext()) { try { var id = cursor.GetInt(cursor.GetColumnIndex(MediaStore.Files.FileColumns.Id)); var mediaType = cursor.GetInt(cursor.GetColumnIndex(MediaStore.Files.FileColumns.MediaType)); Bitmap bitmap = null; switch (mediaType) { case (int)MediaType.Image: bitmap = MediaStore.Images.Thumbnails.GetThumbnail(MainActivity.FormsActivity.ContentResolver, id, ThumbnailKind.MiniKind, new BitmapFactory.Options() { InSampleSize = 4, InPurgeable = true }); break; case (int)MediaType.Video: bitmap = MediaStore.Video.Thumbnails.GetThumbnail(MainActivity.FormsActivity.ContentResolver, id, VideoThumbnailKind.MiniKind, new BitmapFactory.Options() { InSampleSize = 4, InPurgeable = true }); break; } var tmpPath = System.IO.Path.GetTempPath(); var name = GetString(cursor, MediaStore.Files.FileColumns.DisplayName); var filePath = System.IO.Path.Combine(tmpPath, $"tmp_{name}"); var path = GetString(cursor, MediaStore.Files.FileColumns.Data); //filePath = path; using (var stream = new FileStream(filePath, FileMode.Create)) { bitmap?.Compress(Bitmap.CompressFormat.Png, 100, stream); stream.Close(); } if (!string.IsNullOrWhiteSpace(filePath)) { var asset = new MediaAssest() { Id = $"{id}", Type = mediaType == (int)MediaType.Video ? MediaAssetType.Video : MediaAssetType.Image, Name = name, PreviewPath = filePath, Path = path, IsSelectable = true }; using (var h = new Handler(Looper.MainLooper)) h.Post(async() => { OnMediaAssetLoaded?.Invoke(this, new Model.MediaEventArgs(asset)); }); assets.Add(asset); } if (stopLoad) { break; } } catch (Exception ex) { await UserDialogs.Instance.AlertAsync(ex.StackTrace.ToString(), "error", "ok"); } } } }); } return(assets); }
async void imageTapped(System.Object sender, System.EventArgs e) { var s = (StackLayout)sender; var ss = s.Children[0] as Grid; var sss = ss.Children[0] as Frame; selectedFrame = ss.Children[1] as Frame; var clicked = (TappedEventArgs)e; var mediaAssest = (MediaAssest)clicked.Parameter; selectedMediaAsset = mediaAssest; if (mediaAssest.PreviewPath == "group.png") { mainPageViewModel.CancelMediaAssests(); var res = await mediaService.GetImageWithCamera(); if (res != "") { bodyContent.Opacity = 1; bodyContent.InputTransparent = false; imageselector.IsVisible = false; string path = await Xamarin.Forms.DependencyService.Get <IMediaService>().StoreProfileImage(res); //store the image in app folder Preferences.Set("ProfileImage", path); //set the path of the image in preferences ImageSkipOrSelectImageClickEvent(); } } else { if (currentSelectedFrame != null) { if (selectedFrame != currentSelectedFrame) { selectedFrame.BackgroundColor = Color.Green; currentSelectedFrame.BackgroundColor = Color.Transparent; currentSelectedFrame = selectedFrame; isSelected = true; } else { if (selectedFrame.BackgroundColor == Color.Green) { selectedFrame.BackgroundColor = Color.Transparent; currentSelectedFrame = selectedFrame; isSelected = false; } else { selectedFrame.BackgroundColor = Color.Green; isSelected = true; } } } else { selectedFrame.BackgroundColor = Color.Green; currentSelectedFrame = selectedFrame; isSelected = true; } //display next button if (isSelected) { //display next button imageNext.IsVisible = true; await imageNext.TranslateTo(0, 0, 300); } else { //display skip options imageNext.IsVisible = false; await imageNext.TranslateTo(100, 0, 300); } } }