/// <summary> /// /// </summary> /// <param name="self"></param> /// <param name="context"></param> /// <returns></returns> public static Task <MediaFile> GetMediaFileExtraAsync(this Intent self, Context context) { if (self == null) { throw new ArgumentNullException("self"); } if (context == null) { throw new ArgumentNullException("context"); } string action = self.GetStringExtra("action"); if (action == null) { throw new ArgumentException("Intent was not results from MediaPicker", "self"); } var uri = (Android.Net.Uri)self.GetParcelableExtra("MediaFile"); bool isPhoto = self.GetBooleanExtra("isPhoto", false); var path = (Android.Net.Uri)self.GetParcelableExtra("path"); bool saveToAlbum = false; try { saveToAlbum = (bool)self.GetParcelableExtra("album_save"); } catch { } return(MediaPickerActivity.GetMediaFileAsync(context, 0, action, isPhoto, ref path, uri, saveToAlbum) .ContinueWith(t => t.Result.ToTask()).Unwrap()); }
/// <summary> /// Take a photo async with specified options /// </summary> /// <param name="options">Camera Media Options</param> /// <returns>Media file of photo or null if canceled</returns> public async Task <MediaFile> TakePhotoAsync(StoreCameraMediaOptions options) { if (!IsCameraAvailable) { throw new NotSupportedException(); } if (!(await RequestStoragePermission())) { return(null); } VerifyOptions(options); var media = await TakeMediaAsync("image/*", MediaStore.ActionImageCapture, options); if (string.IsNullOrWhiteSpace(media?.Path)) { return(media); } if (options.SaveToAlbum) { try { var fileName = System.IO.Path.GetFileName(media.Path); var publicUri = MediaPickerActivity.GetOutputMediaFile(context, options.Directory ?? "temp", fileName, true, true); using (System.IO.Stream input = File.OpenRead(media.Path)) using (System.IO.Stream output = File.Create(publicUri.Path)) input.CopyTo(output); media.AlbumPath = publicUri.Path; var f = new Java.IO.File(publicUri.Path); //MediaStore.Images.Media.InsertImage(context.ContentResolver, // f.AbsolutePath, f.Name, null); try { Android.Media.MediaScannerConnection.ScanFile(context, new[] { f.AbsolutePath }, null, context as MediaPickerActivity); ContentValues values = new ContentValues(); values.Put(MediaStore.Images.Media.InterfaceConsts.Title, System.IO.Path.GetFileNameWithoutExtension(f.AbsolutePath)); values.Put(MediaStore.Images.Media.InterfaceConsts.Description, string.Empty); values.Put(MediaStore.Images.Media.InterfaceConsts.DateTaken, Java.Lang.JavaSystem.CurrentTimeMillis()); values.Put(MediaStore.Images.ImageColumns.BucketId, f.ToString().ToLowerInvariant().GetHashCode()); values.Put(MediaStore.Images.ImageColumns.BucketDisplayName, f.Name.ToLowerInvariant()); values.Put("_data", f.AbsolutePath); var cr = context.ContentResolver; cr.Insert(MediaStore.Images.Media.ExternalContentUri, values); } catch (Exception ex1) { Console.WriteLine("Unable to save to scan file: " + ex1); } var contentUri = Android.Net.Uri.FromFile(f); var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile, contentUri); context.SendBroadcast(mediaScanIntent); } catch (Exception ex2) { Console.WriteLine("Unable to save to gallery: " + ex2); } } //check to see if we need to rotate if success try { await FixOrientationAndResizeAsync(media.Path, options.PhotoSize, options.CompressionQuality, options.CustomPhotoSize); } catch (Exception ex) { Console.WriteLine("Unable to check orientation: " + ex); } return(media); }
private void SaveMediaToAlbum(StoreCameraMediaOptions options, MediaFile media) { var defImage = new Dictionary <string, string> { { "Title", MediaStore.Images.Media.InterfaceConsts.Title }, { "Description", MediaStore.Images.Media.InterfaceConsts.Description }, { "DateTaken", MediaStore.Images.Media.InterfaceConsts.DateTaken }, { "BucketId", MediaStore.Images.ImageColumns.BucketId }, { "BucketDisplayName", MediaStore.Images.ImageColumns.BucketDisplayName }, }; var defVideo = new Dictionary <string, string> { { "Title", MediaStore.Video.Media.InterfaceConsts.Title }, { "Description", MediaStore.Video.Media.InterfaceConsts.Description }, { "DateTaken", MediaStore.Video.Media.InterfaceConsts.DateTaken }, { "BucketId", MediaStore.Video.VideoColumns.BucketId }, { "BucketDisplayName", MediaStore.Video.VideoColumns.BucketDisplayName }, }; var definitions = new Dictionary <Abstractions.MediaType, Dictionary <string, string> > { { Abstractions.MediaType.Image, defImage }, { Abstractions.MediaType.Video, defVideo }, }; try { Console.WriteLine("saving to gallery"); var fileName = System.IO.Path.GetFileName(media.Path); var publicUri = MediaPickerActivity.GetOutputMediaFile(context, options.Directory ?? "temp", fileName, true, true); using (Stream input = File.OpenRead(media.Path)) using (Stream output = File.Create(publicUri.Path)) { input.CopyTo(output); } media.AlbumPath = publicUri.Path; var f = new Java.IO.File(publicUri.Path); try { Android.Media.MediaScannerConnection.ScanFile(context, new[] { f.AbsolutePath }, null, context as MediaPickerActivity); var values = new ContentValues(); values.Put(definitions[media.Type]["Title"], System.IO.Path.GetFileNameWithoutExtension(f.AbsolutePath)); values.Put(definitions[media.Type]["Description"], string.Empty); values.Put(definitions[media.Type]["DateTaken"], Java.Lang.JavaSystem.CurrentTimeMillis()); values.Put(definitions[media.Type]["BucketId"], f.ToString().ToLowerInvariant().GetHashCode()); values.Put(definitions[media.Type]["BucketDisplayName"], f.Name.ToLowerInvariant()); values.Put("_data", f.AbsolutePath); var cr = context.ContentResolver; cr.Insert(media.Type == Abstractions.MediaType.Image ? MediaStore.Images.Media.ExternalContentUri : MediaStore.Video.Media.ExternalContentUri, values); } catch (Exception ex1) { Console.WriteLine("Unable to save to scan file: " + ex1); } var contentUri = Android.Net.Uri.FromFile(f); var mediaScanIntent = new Intent(Intent.ActionMediaScannerScanFile, contentUri); context.SendBroadcast(mediaScanIntent); } catch (Exception ex2) { Console.WriteLine("Unable to save to gallery: " + ex2); } }
/// <summary> /// Take a photo async with specified options /// </summary> /// <param name="options">Camera Media Options</param> /// <param name="token">Cancellation token</param> /// <returns>Media file of photo or null if canceled</returns> public async Task <MediaFile> TakePhotoAsync(StoreCameraMediaOptions options, CancellationToken token = default(CancellationToken)) { if (!IsCameraAvailable) { throw new NotSupportedException(); } if (!(await RequestCameraPermissions())) { throw new MediaPermissionException(nameof(Permissions.Camera)); } VerifyOptions(options); var media = await TakeMediaAsync("image/*", MediaStore.ActionImageCapture, options, token); if (string.IsNullOrWhiteSpace(media?.Path)) { return(media); } if (options.SaveToAlbum) { var fileName = System.IO.Path.GetFileName(media.Path); var uri = MediaPickerActivity.GetOutputMediaFile(context, options.Directory ?? "temp", fileName, true, true); var f = new Java.IO.File(uri.Path); media.AlbumPath = uri.ToString(); try { var values = new ContentValues(); values.Put(MediaStore.Audio.Media.InterfaceConsts.DisplayName, System.IO.Path.GetFileNameWithoutExtension(f.AbsolutePath)); values.Put(MediaStore.Audio.Media.InterfaceConsts.MimeType, "image/jpeg"); values.Put(MediaStore.Images.Media.InterfaceConsts.Description, string.Empty); values.Put(MediaStore.Images.Media.InterfaceConsts.DateTaken, Java.Lang.JavaSystem.CurrentTimeMillis()); values.Put(MediaStore.Images.ImageColumns.BucketId, f.ToString().ToLowerInvariant().GetHashCode()); values.Put(MediaStore.Images.ImageColumns.BucketDisplayName, f.Name.ToLowerInvariant()); var cr = context.ContentResolver; var albumUri = cr.Insert(MediaStore.Images.Media.ExternalContentUri, values); using (System.IO.Stream input = File.OpenRead(media.Path)) using (System.IO.Stream output = cr.OpenOutputStream(albumUri)) input.CopyTo(output); } catch (Exception ex) { Console.WriteLine("Unable to save to gallery: " + ex); } } //check to see if we need to rotate if success try { var exif = new ExifInterface(media.Path); if (options.RotateImage) { await FixOrientationAndResizeAsync(media.Path, options, exif); } else { await ResizeAsync(media.Path, options, exif); } if (options.SaveMetaData && IsValidExif(exif)) { SetMissingMetadata(exif, options.Location); try { exif?.SaveAttributes(); } catch (Exception ex) { Console.WriteLine($"Unable to save exif {ex}"); } } exif?.Dispose(); } catch (Exception ex) { Console.WriteLine("Unable to check orientation: " + ex); } return(media); }